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 for maxSdkVersion > 18
when writing to external directories, why doesn't it require it here with DownloadManager
?
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 your getExternalFilesDir()
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 that DownloadManager
talks to — is requiring you to have this permission, and it would appear to do so without regard to API level.
The doc for DownloadManager.Request clearly mentions that the destination you set for any of the setDestination
methods must be on external storage and that your app must have WRITE_EXTERNAL_STORAGE
permission:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With