DownloadManager不适用于Android 10(Q) [英] DownloadManager not working for Android 10 (Q)
问题描述
针对这个问题,我已经花了很长时间了...我正在更新一个使用DownloadManger来执行简单任务的应用程序,例如将文件下载到外部存储公共目录,即:
I've been beating my head against this issue for quite awhile... I am updating an app that uses DownloadManger to do a simple task like downloading a file to the external storage public directory i.e:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
一切都可以从Android api 19-28正常运行.在API 29(Q/10)上进行测试时会出现问题.Android实现了范围存储,因此不赞成使用getExternalStoragePublicDirectory ...因此,我需要找出一个兼容的解决方案来支持API 19- 29 .我无法使用内部应用程序存储,因为DownloadManager会引发SecurityException.Android的文档指出,我可以使用DownloadManager.Request setDestinationUri
,对于Android Q,它甚至提到我可以使用 Context.getExternalFilesDir(String)
.但是,当我执行此操作时,该路径仍然是模拟路径:
Everything works fine here from Android api 19-28. Its when testing on API 29 (Q/10) is where issues occur. Android implemented scoped storage and so deprecated the getExternalStoragePublicDirectory... As a result I need to figure out a compatible solution to support APIs 19-29. I cannot use internal application storage since DownloadManager will throw a SecurityException. Androids documentation states that I can use the DownloadManager.Request setDestinationUri
and it even mentions for Android Q that I can use Context.getExternalFilesDir(String)
. When I do this though, the path is still the emulated path:
/storage/emulated/0/Android/data/com.my.package.name/files/Download/myFile.xml
我从下载管理器收到一个回调,告知下载已完成(具有正确的ID),但是我无法从保存它的区域中获取下载.我检查文件是否存在,并返回false:
I get a callback from the download manager that the download is complete (with right ID) but then I cannot grab the download from the area I saved it to. I check to see if the file exists and it returns false:
new File("/storage/emulated/0/Android/data/com.my.package.name/files/Download/myFile.xml").exists();
感谢您的帮助
添加上下文代码.因此,设置下载管理器
Adding code for context. So setting up download manager
private void startDownload() {
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, filter);
String remoteURL= getString(R.string.remote_url);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(remoteUrl));
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setTitle(getString(R.string.download_title));
request.setDescription(getString(R.string.download_description));
request.setDestinationUri(Uri.fromFile(new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "myFile.xml")));
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
mainDownloadID= manager.enqueue(request);
}
检查文件是否存在:
new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "myFile.xml").exists(); //this returns false in the onReceive (and download IDs match)
推荐答案
尝试将其添加到应用程序标记中的清单文件中
Try add this into your manifest file in application tag
android:requestLegacyExternalStorage ="true"
android:requestLegacyExternalStorage="true"
这篇关于DownloadManager不适用于Android 10(Q)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!