JustASimpleLineOfCode
JustASimpleLineOfCode

Reputation: 35

Why don't we need storage permission to download a file through DownloadManager

I'm downloading a file through android DownloadManager with de function below.

 private fun downloadFile() {
        val downloadManager: DownloadManager =
            getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

        val request =
            DownloadManager.Request(Uri.
            parse("https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"))

        downloadManager.enqueue(request)
    }
}

The download occurs normally without any storage permissions.

Does the download manager work without storage permissions? I can't find the answer in the official documentation.

https://developer.android.com/reference/android/app/DownloadManager

Upvotes: 1

Views: 3458

Answers (2)

kubas500
kubas500

Reputation: 33

If u use setDestinationInExternalPublicDir

For applications targeting Build.VERSION_CODES.Q or above, WRITE_EXTERNAL_STORAGE permission is not needed and the dirType must be one of the known public directories like Environment#DIRECTORY_DOWNLOADS, Environment#DIRECTORY_PICTURES, Environment#DIRECTORY_MOVIES, etc.

And for setDestinationUri

For applications targeting Build.VERSION_CODES.Q or above, WRITE EXTERNAL_STORAGE permission is not needed and the uri must refer to a path within the directories owned by the application (e.g. Context#getExternalFilesDir(String)) or a path within the top-level Downloads directory (as returned by Environment#getExternalStoragePublicDirectory(String) with Environment#DIRECTORY_DOWNLOADS).

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006664

DownloadManager delegates the downloading work to a separate system-supplied app. That app has rights to write to a few locations, even if your app does not.

Upvotes: 5

Related Questions