Revisiting Android Scoped Storage in 2021

Thach Do
5 min readJan 30, 2021

--

One peaceful afternoon in Melbourne — Photo by me

Android 11 has been released for a few months and manufacturers are dispatching it to their users. As usual, there are new cool things for developers to catch up on. Among those, scoped storage stands out telling me about my obsolete skills when it comes to file operations.

I decided to take a proper look at the new storage paradigm to make myself up-to-date again. These are questions that I asked myself before jumping in:

  1. Where to store files? Are internal storage / external storage still there in Android 11?
  2. What permissions are required to read / write files?
  3. How to perform file operations in the new fashion?
  4. What are the exceptions?

Before we begin, let me remind myself and you about the term “storage”. It is not about the built-in storage that come with your device nor external SD cards. Those hardwares are referred to as volumes in all Android documentation.

  • The built-in storage that you can easily find on any Android device specs is called “primary volume”.
  • External SD cards are “secondary volume”.

Storage is the result of Android OS partitioning physical volumes.

1. Where to store files?

Internal storage and external storage are still there for us. However, they’re known by different names.

  • Internal storage is now app-specific storage.
  • External storage is now shared storage.

More details can be found here.

2. What permissions are required to read / write files?

Your app doesn’t need any permission nor user consent to read and write files in its app-specific storage.

In shared storage, your app might or might not need permissions to perform file operations depending on what files it is trying to access and its ownership to those files.

There’re 2 kinds of files in shared storage: media files and non-media files. Media files are files located in predefined Media directories and Download directory. Non-media files are files located elsewhere.

Files in the below directories can be considered media files. You might want to check out the list of media directories here at the time you read this article.

DCIM/, Pictures/, Movies/, Alarms/, Audiobooks/, Music/, Notifications/, Podcasts/, Ringtones/, Download/

Shared storage treats media files and non-media files differently. Let’s explore them one by one.

a. Media files

  • Adding new files.

Apps don’t need any permission to add media files to shared storage.

Yes, it’s true. You can find out more in the official document.

  • Reading is where the ownership kicks in.

If your app is reading a media file that it created, no permission is required.

If a media file is created by other apps, your app needs to ask for READ_EXTERNAL_STORAGE permission before accessing it.

Details explanation here.

  • Modifying / deleting is a bit different from reading operation.

Your app doesn’t need any permission to modify nor delete media files that it has created.

What if a media is created by other apps? No permission is required either. However, you need to ask for user consent by using the MediaStore API.

b. Non-media files

When working with non-media files, you don’t need permissions. Instead, you need user consent which can be granted by using Storage Access Framework.

Using Storage Access Framework for file / directory picker

The above screenshots demonstrate how users can share access to directories / files with your app using Storage Access Framework (SAF). And guess what, here is all the code you need to write to achieve that.

3. How to perform file operations in the new fashion?

So far, I have mentioned 2 ways of working with files in shared storage:

  • MediaStore API
  • Storage Access Framework

However, there are libraries that still use the traditional File API to perform operations. If your app is relying on such libraries, no worries at all as scoped storage supports direct access to files using File API or even fopen(). More details here.

4. What are the exceptions?

If you have followed that far, you might be thinking that there’s no such perfect API in the Android world due to the famous fragmentation and backward compatibility.

You’re absolutely right! Those exceptions are caused by the mismatch between targetSdkVersion and the actual Android version installed on devices. However, I’ll not cover those exceptions here as Android team has covered that part perfectly in this guideline.

Closing thoughts

I love the idea of apps having full control over files that they have created. Developers no longer need to worry about cases where users reject their requests for accessing files created by their own apps.

By the way, there’re a few things that I have left out on purpose:

  • Migration strategy. I haven’t migrated any apps to using Scope Storage yet, so I’m not giving any thoughts here.
  • Starting from API 29, if your app wants to get location information from any photos, it needs to ask for ACCESS_MEDIA_LOCATION permission regardless whether it is the owner of the photos or not.
  • With Scoped Storage, Android is trying to limit board access to shared storage. If you wish to develop an app that needs access to all files on shared storage, you need to ask for MANAGE_EXTERNAL_STORAGE permission. Plus, in order to publish that app to Google Play, you have to do some extra steps to comply with its policy. Check out this document for more details.

--

--

Thach Do
Thach Do

Written by Thach Do

Senior Android Developer @OKX // I Build Android Web3 Wallet

No responses yet