这是我第一次尝试实现DownloadManager
,无论我尝试什么,我总是收到一条通知,说“下载失败。”我看过许多其他的SO论坛,一些教程以及我应该使用的内容。是的,我已经在清单文件中设置了Internet和外部存储权限。是的,我已经在手机的应用程序设置中授予了存储权限。我已经在运行API 28的Android模拟器和运行相同API的真实手机上进行了尝试。这是我的代码:
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中使用的是不安全的网址,那么它将无法执行您的网址。检查Official Documentation。
避免明文流量的原因是缺乏机密性,真实性和防止篡改的保护;网络攻击者可以窃听传输的数据,也可以对其进行修改而无需检测到。
在清单中添加以下内容以绕过所有安全性。
<application
android:name=".ApplicationClass"
....
android:usesCleartextTraffic="true">