androidandroid-download-managerandroid-sdk-2.3download-managerandroid-version

Download manager code is working in older device and not in latest device


Below is my code which i integrated to download the video(.mp4) functionality. This code is working in my Android 8 device. The same code is not working in latest Android version. May be i am missing something or is there any deprecation issue?

 val dm = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
                val url =
                    "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"
                val request = DownloadManager.Request(
                    Uri.parse(url)
                )
                fileName = url.substring(url.lastIndexOf('/') + 1)
                fileName =
                    fileName.substring(0, 1).uppercase(Locale.getDefault()) + fileName.substring(1)
                request.setDestinationInExternalFilesDir(
                    this,
                    Environment.DIRECTORY_DOWNLOADS,
                    fileName
                )
                enqueue = dm?.enqueue(request)!!

I tried debugging, looked for if there is any issue regarding the URL but there aren't any and also tried for the error logs but there aren't any error for the same.


Solution

  • The reason for this is because, I am using http url for the video to download and not https(secure) using DownloadManager and i haven't mentioned android:usesCleartextTraffic as true in manifest. So the download manager is not working for the security reason as http url is not secure in terms of the encryption over the internet call. userCleartextTraffic means whether the application intend to use the clear text traffic (unencrypted network traffic) or not.

    So, when i mentioned android:usesCleartextTraffic as true in manifest it started working with the http url from the Android 9.0(API level 28) and higher because below this version there is no tag introduced and simply it will get ignored hence not required and it will works below this version.