Android 11: Scoped Storage

D'haval Asodariya
DhiWise
Published in
3 min readJul 21, 2021
Photo by Denny Müller on Unsplash

Privacy has always been a major consideration for smartphones, but over the past two years or so, it has become a key component in Android updates — Android 9 and 10 both introduced breaking changes designed to better protect users.

In Android 11, Google drastically limits which directories apps can access on phone as part of its new Scoped Storage.

That means, Apps are only allowed to access files in external storage that they had created themselves (preferably contained within an app-specific directory), and audio, image, and video files contained within the Music, Pictures, or Videos directories. Any other file may only be accessed via user intervention through the backward-incompatible Google Storage Access Frameworks.

The impending new ruling will purge many bad apps that use the permission to spy on you or to grab your data, as Google has to curate which apps will receive the exemption and which won’t.

So here are some best practices to develop applications without breaking any rules of managing storage permission.

To store shared media files :

For apps that handle files that can be sharable with other apps (such as photos, video) and be retained after the app has been uninstalled, use the MediaStore API. There are certain collections for common media files: Audio, Video, and Images. For other file types, you can store them in the Downloads directory. To access files from the Downloads directory, apps must use the system picker.

To store app-internal files :

If your app is having files that are not meant to be shared with other apps, store them in your package-specific directories. This helps keep files organized and limit file clutter as the OS will manage cleanup when the app is uninstalled. Calls to functionContext.getExternalFilesDir() will continue to work.

Working with permissions and file ownership :

For MediaStore, no permissions are necessary for apps that only access their own files. Your app will need to request permission to access that media which is contributed by other apps.

So in common words, no READ_EXTERNAL_STORAGE is required to access the app's own files. But it is necessary when accessing files that have been created by other applications.

Refer to the below table for a quick summary.

Image credit: https://speakerdeck.com/cmota

Starting in Android 11, apps cannot create their own app-specific directory on external storage. To access the directory that the system provides for your app, you need to call Context.getExternalFilesDirs()

If your app targets Android 11, it cannot access the files in any other app’s data directory, even if the other app targets Android 8.1 (API level 27) or lower and has made the files in its data directory world-readable.

See the below image to get a better idea of Scoped Storage.

Conclusion :

Scoped Storage introduces breakable changes to the way Android apps work with files. The important thing to keep in mind is, to directly access files, you can’t use the File APIs anymore. Instead, you can rely on the Storage Access Framework for choosing files or folders, and the MediaStore for media files.

I hope the details shown in this post have been helpful to you and it’s a good idea to have your app ready as soon as possible.

Useful links :

Storage updates in Android 11 :

developer.android.com/preview/privacy/storage

Preparing for Scoped Storage :

raywenderlich.com/10217168-preparing-for-scoped-storage

Storage access with Android 11 :

youtube.com/watch?v=RjyYCUW-9tY

Android 11 Storage FAQ :

medium.com/androiddevelopers/android-11-storage-faq-78cefea52b7c

--

--

D'haval Asodariya
DhiWise

SDE-III at DhiWise 🤓 | Kotlin Expert 💻 | Android Enthusiastic 📱