android - Android 下载管理器“下载失败”
问题描述
这是我第一次尝试实施DownloadManager
,无论我尝试什么,我总是收到一条通知,说“下载不成功”。我查看了许多其他 SO 论坛、一些教程以及我应该工作的内容。是的,我在清单文件中设置了互联网和外部存储权限。是的,我已经在手机的应用设置中授予了存储权限。我在运行 API 28 的 Android 模拟器和运行 API 28 的真机上都试过这个。这是我的代码:
String url = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4";
DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setTitle("title");
request.setDescription("Your file is downloading");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, "" + System.currentTimeMillis());
request.allowScanningByMediaScanner();
request.setAllowedOverMetered(true);
request.setAllowedOverRoaming(true);
//Enqueue download and save the referenceId
long downloadReference = downloadManager.enqueue(request);
if (downloadReference != 0) {
Toast.makeText(getApplicationContext(), "download started", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getApplicationContext(), "no download started", Toast.LENGTH_SHORT).show();
}
任何帮助或建议表示赞赏。谢谢。
解决方案
由于网络安全,会出现此问题。如果您在上面的 pie API 中使用不安全的 url,则它无法执行您的 url。检查官方文档。
避免明文流量的原因是缺乏机密性、真实性和防篡改保护;网络攻击者可以窃听传输的数据,也可以在不被发现的情况下对其进行修改。
在清单中添加以下内容以绕过所有安全性。
<application
android:name=".ApplicationClass"
....
android:usesCleartextTraffic="true">
推荐阅读
- extjs - Guidewire PolicyCenter 列 minWidth
- unix - 获取“expr”的语法错误异常
- python - 文本聚类:质心列表中的重复项
- python - 将图像保存为 jpg 给我纯黑色
- promise - WebdriverIO 和 teamcity 构建设置,对象承诺问题
- dataframe - foreachbatch 是不是动作运算符?
- android - Android Studio 中的 OPEN_APP_FEATURE 操作
- ios - React Native xcode iOS 11.0 部署目标问题
- javascript - 多个复选框选择结果
- c# - 如何延迟代码块的运行?