Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why download manager requires `WRITE_EXTERNAL_STORAGE` for maxSdkVersion > 18

Tags:

android

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)

like image 516
Max Koretskyi Avatar asked May 25 '15 07:05

Max Koretskyi


2 Answers

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.

like image 178
CommonsWare Avatar answered Oct 25 '22 07:10

CommonsWare


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:

like image 31
Kartheek Avatar answered Oct 25 '22 08:10

Kartheek