Storage, Permissions, and Access
Android 4.4 through 9.0 had a fairly stable set of rules for when you need permissions:
Storage Type | Storage Methods | Permissions Required |
---|---|---|
internal | methods on Context |
none |
external | methods on Context |
none |
external | methods on Environment |
READ_EXTERNAL_STORAGE , WRITE_EXTERNAL_STORAGE |
removable | methods on Context |
none |
So, only if you access shared locations on external storage do you need permission. As the names suggest, READ_EXTERNAL_STORAGE
allows you to read those files, while WRITE_EXTERNAL_STORAGE
allows you to write those files. These are dangerous permissions, so you need to request them both in the manifest and at runtime, as we saw in the chapter on permissions.
Android 10 changed this. You simply have no filesystem-level access to external or removable storage except by Context
methods like getExternalFilesDir()
. Everything else is locked down by default. To revert this behavior, you can add android:requestLegacyExternalStorage="true"
to the <application>
element in your manifest. This attribute will give you the Android 9.0-style behavior on:
- Android 10, and
- Android 11, until you raise your
targetSdkVersion
to 30 or higher
However, on Android 11+, you get read access to much of external storage using READ_EXTERNAL_STORAGE
as before. What you lose is write access. For apps that only need to read from shared locations on external storage, having android:requestLegacyExternalStorage="true"
means that you will have fairly consistent behavior through the Android versions.
Android 12 — introduced in 2021 — ignores android:requestLegacyExternalStorage="true"
entirely.
(and, if this all seems overly complicated… it is)
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.