这是我第一次尝试实现DownloadManager
,无论我尝试什么方法,我总是收到一个通知说“下载失败”。我查看了许多其他SO论坛、几个教程,我所拥有的应该可以工作。是的,我在清单文件中设置了网络和外部存储权限。是的,我在手机的应用程序设置中授予了存储权限。我在运行API 28的Android模拟器和运行相同版本的真实手机上都尝试过这个。下面是我的代码:
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();
}
非常感谢您提供的任何帮助或建议。谢谢。