下载管理器返回的Android 9.0下载失败

时间:2018-11-15 16:25:24

标签: android android-download-manager android-9.0-pie

我正在使用下载管理器从Internet下载文件。下载已在Android 6、8.1中成功完成,但在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);

2 个答案:

答案 0 :(得分:2)

我在这里找到了解决方案:How to solve Android P DownloadManager stopping with "Cleartext HTTP traffic to 127.0.0.1 not permitted"?

简而言之,我只需要执行以下操作:

1。创建res / xml / network_security_config.xml,然后添加

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

2。在清单文件中,在应用程序标记内引用它

android:networkSecurityConfig="@xml/network_security_config"

有关更多信息,可以在这里阅读:https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted

答案 1 :(得分:2)

此标签对我有用

 android:usesCleartextTraffic="true"

将此标签添加到应用程序中

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