问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/50286117/android-p-download-does-not-start-using-download-manager