4

I am trying to download files using the download manager and the download does not seem to be starting on Android P (running on the emulator). It works fine on Android Oreo and Android Marshmallow. Here is the code.

DownloadManager.Request downloadRequest = new DownloadManager.Request(Uri.parse(URLS.URLforDownloadingFileData(fileName)));
        File file = new File(mobileInfo.getExternalFileStorageDirectory(),fileName);
        downloadRequest.setDestinationUri(Uri.fromFile(file));
        downloadRequest.setTitle(titleOfDownload);
        downloadRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        downloadManager.enqueue(downloadRequest);

Any idea why this could be happening and how to solve this problem? Anything to do with changes introduced in Android P?

Sagar
  • 23,903
  • 4
  • 62
  • 62
Dishonered
  • 8,449
  • 9
  • 37
  • 50

1 Answers1

4

Seems like you have define NetworkSecurityPolicy for downloading urls host. You can check it by creating of XML res/xml/network_security_config.xml:

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

And then reference this file in Application tag inside of your AndroidManifest.xml:

android:networkSecurityConfig="@xml/network_security_config"

If it works - it's strongly recommended to permit trafic not for all hosts, but only for needed.

Community
  • 1
  • 1
A. Shevchuk
  • 2,109
  • 14
  • 23