首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DownloadManager不为安卓10工作(Q)

DownloadManager不为安卓10工作(Q)
EN

Stack Overflow用户
提问于 2019-12-04 13:27:01
回答 4查看 9.8K关注 0票数 10

我在这件事上花了好长时间.我正在更新一个使用DownloadManger执行简单任务的应用程序,比如将文件下载到外部存储公共目录,即:

代码语言:javascript
复制
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)

从Android 19到28,一切都很好。在API 29 (Q/10)上进行测试时会出现问题。Android实现了作用域存储,因此不推荐getExternalStoragePublicDirectory.因此,我需要找到一个兼容的解决方案来支持API 19-29。我不能使用内部应用程序存储,因为DownloadManager会抛出一个SecurityException。Android文档声明我可以使用DownloadManager.Request setDestinationUri,它甚至提到了Android可以使用Context.getExternalFilesDir(String)。但是,当我这样做时,路径仍然是模拟路径:

代码语言:javascript
复制
/storage/emulated/0/Android/data/com.my.package.name/files/Download/myFile.xml

我从下载管理器得到一个回调,说明下载已经完成(有正确的ID),但是我无法从我保存到的区域获取下载。我检查该文件是否存在,并返回false:

代码语言:javascript
复制
new File("/storage/emulated/0/Android/data/com.my.package.name/files/Download/myFile.xml").exists();

任何帮助都是非常感谢的。

添加上下文代码。所以设置下载管理器

代码语言:javascript
复制
    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);
    }

检查文件是否存在:

代码语言:javascript
复制
new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "myFile.xml").exists(); //this returns false in the onReceive (and download IDs match)
EN

回答 4

Stack Overflow用户

发布于 2019-12-04 13:47:06

尝试将其添加到应用程序标记中的清单文件中。

Android:requestLegacyExternalStorage=“真”

票数 1
EN

Stack Overflow用户

发布于 2019-12-04 13:49:49

在Android及以上的应用程序私有目录之外的文件路径是无用的。

请参阅https://developer.android.com/training/data-storage#scoped-storage

您还需要询问用户在哪里下载文件,这将为DownloadManager目的地获得一个URI。

https://developer.android.com/training/data-storage/shared/documents-files#grant-access-directory

您可能希望持久化此权限。

https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions

票数 0
EN

Stack Overflow用户

发布于 2021-01-23 18:01:59

是的,它的作用域存储,但是即使您可以使用下载管理器在Q+中下载文件,也不需要执行android:requestLegacyExternalStorage="true"

我就是这样做的。

  1. manifest
  • -->
  1. Downloadmanger

val fileName = Constants.FILE_NAME + Date().time val downloadUri = Uri.parse(media.url) val request = DownloadManager.Request( downloadUri ) request.setAllowedNetworkTypes( DownloadManager.Request.NETWORK_WIFI或DownloadManager.Request.NETWORK_MOBILE ).setAllowedOverRoaming(真).setTitle(真).setDescription(“下载文件”) .setDestinationInExternalPublicDir( Environment.DIRECTORY_DOWNLOADS,File.separator +文件夹+ File.separator + fileName ) Toast.makeText(上下文,“成功下载到${downloadUri?.path}",Toast.LENGTH_LONG ).show() Toast.makeText

因此,它将请求Q以下的写权限,但在Q和Q+中,它将在/ download /文件夹dir中下载而无需请求许可。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59177136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档