为什么下载管理器需要为`&maxSdkVersion GT WRITE_EXTERNAL_STORAGE`; 18 [英] Why download manager requires `WRITE_EXTERNAL_STORAGE` for maxSdkVersion > 18
问题描述
我用下载管理器
来下载文件并将其保存到这样的外部存储:
DownloadManager.Request REQ =新DownloadManager.Request(URI);
req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setDestinationInExternalFilesDir(TextReaderActivity.this,空,播客/+ Integer.toString(article.id)+ PODCAST_EXTENSION);
我得到了以下异常:
例外:需要WRITE_EXTERNAL_STORAGE许可使用DESTINATION_FILE_URI:UID 10052没有android.permission.WRITE_EXTERNAL_STORAGE。
我读过,我并不需要请求 WRITE_EXTERNAL_STORAGE
权限 maxSdkVersion> 18
写外部目录时,为什么没有要求它在这里与下载管理器
?
更新:
在Android 4.4以上版本,规则是有点不同:结果
- 要阅读或在getExternalFilesDir根的目录树写()
和getExternalCacheDir(),则不需要许可结果
- 要写到其他地方上的外部存储,你需要WRITE_EXTERNAL_STORAGE结果
- 要从其他地方上的外部存储读取,您需要使用
WRITE_EXTERNAL_STORAGE(如果你已经有一个)或
READ_EXTERNAL_STORAGE(如果不是)
块引用>解决方案这是你举有关的规则
WRITE_EXTERNAL_STORAGE
是时的您的流程的写到外部存储,然后只为你的getExternalFilesDir()
及相关目录。在这种情况下,你是不是写到外部存储。你问
下载管理器
写到外部存储。下载管理器
—或者更准确地说,在过程下载管理器
会谈—是需要你有这样的权限,它会出现不考虑API级别这样做。I'm using
Download manager
to download file and save it to the external storage like this:DownloadManager.Request req = new DownloadManager.Request(uri); req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE) .setDestinationInExternalFilesDir(TextReaderActivity.this, null, "podcasts/" + Integer.toString(article.id) + PODCAST_EXTENSION);
I get the following exception:
Exception: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10052 does not have android.permission.WRITE_EXTERNAL_STORAGE.
I've read that I don't need to request
WRITE_EXTERNAL_STORAGE
permission formaxSdkVersion > 18
when writing to external directories, why doesn't it require it here withDownloadManager
?UPDATE:
On Android 4.4 and up, the rules are a bit different:
- To read or write in the directory trees rooted at getExternalFilesDir() and getExternalCacheDir(), you do not need a permission
- To write to anywhere else on external storage, you need WRITE_EXTERNAL_STORAGE
- To read from anywhere else on external storage, you need either WRITE_EXTERNAL_STORAGE (if you already have that) or READ_EXTERNAL_STORAGE (if not)
解决方案The rules that you cite regarding
WRITE_EXTERNAL_STORAGE
are for when your process writes to external storage, and then only for yourgetExternalFilesDir()
and related directories.In this case, you are not writing to external storage. You are asking
DownloadManager
to write to external storage.DownloadManager
— or, more accurately, the process thatDownloadManager
talks to — is requiring you to have this permission, and it would appear to do so without regard to API level.这篇关于为什么下载管理器需要为`&maxSdkVersion GT WRITE_EXTERNAL_STORAGE`; 18的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!