6

I am using Download Manager to download file from the internet. The download complete successfully in Android 6, 8.1, but not in Android 9.0

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading...");
request.setTitle(nameOfFile);
request.setMimeType("application/pdf");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/CPG", nameOfFile);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
queueId = manager.enqueue(request);
Quick learner
  • 10,632
  • 4
  • 45
  • 55
Ehsan Rosdi
  • 835
  • 9
  • 18

2 Answers2

8

This tag worked for me

 android:usesCleartextTraffic="true"

Add this tag to the application

 <application
        android:name=".ApplicationClass"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:usesCleartextTraffic="true"

Solution 2)

Create a XML res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

in your tag Application inside AndroidManifest.xml

android:networkSecurityConfig="@xml/network_security_config"

Download Manger not working in Android Pie 9.0 (Xiaomi mi A2)

Quick learner
  • 10,632
  • 4
  • 45
  • 55
6

I found the solution through here: How to solve Android P DownloadManager stopping with "Cleartext HTTP traffic to 127.0.0.1 not permitted"?

In short, I just need to do the following:

1.create res/xml/network_security_config.xml, then add

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

2. in manifest file, reference it inside application tag

android:networkSecurityConfig="@xml/network_security_config"

For more info, can read here: https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted

Ehsan Rosdi
  • 835
  • 9
  • 18
  • 1
    This does not work for people who are passing the http URL to download manager, will work for making other http calls in the same app. @Quicklearner were you able to solve it? – Jalpesh Feb 28 '19 at 15:15
  • @Jalpesh look at this question of mine https://stackoverflow.com/questions/53493077/download-manger-not-working-in-android-pie-9-0-xiaomi-mi-a2 – Quick learner Mar 01 '19 at 04:53
  • Sorry, it Looks similar to the solution mentioned above, Am I missing anything? – Jalpesh Mar 01 '19 at 08:04
  • This problem recur after sometime. I had to manually clear the cache for download manager to enable the downloading process again – Ehsan Rosdi Mar 01 '19 at 23:29
  • This saved my day :) – asitis Dec 11 '19 at 07:50
  • @HuyMiracle, perhaps can try to programmatically clear the download manager cache – Ehsan Rosdi Dec 11 '19 at 12:06