All You Need to Know About Scoped Storage in Android 10

The future of in-app storage on Android devices is here

Priyanka Gupta
Mar 11 · 5 min read
Image for post
Image for post
Source

Scoped storage is the newly introduced storage system by Android. But before diving into scoped storage, let us first discuss what shared storage is and its requirements in Android.


Current Scenario

  • All apps have their own private directory in internal storage, i.e Android/data/your package name, not visible to other apps.
  • Most of the current apps require broad storage permissions to perform simple functions. For example, downloading an image or as an image picker, etc. At the time of uninstalling the app, most of the time, files don’t get deleted. This results in insufficient storage.

Android 10 comes up with a solution: scoped storage.


What Is Scoped Storage?

  • It’s a concept of storing files, images, etc. separately, called Collections, which restrict the conventional access of the whole storage.
  • Better attribution: This means that the system knows which app created which file. It is useful when the app is uninstalled, so all the data related to the app is also uninstalled.
  • Protecting app data: Internal app directories and external app directories are private.
  • Protect user data: The downloaded image cannot be used by another app.

Key Features

  1. Unrestricted access to its individual app storage (internal/external): No permission required.
  2. Unrestricted access to media files and download collections: E.g. Save the image file without permission.
  3. Only media collections can be read with storage permissions.
  4. Location metadata ACCESS_MEDIA_LOCATION for the location of the image.
  5. For files like PDFs, text, etc. use System Picker.
  6. Reading and writing outside of a collection requires the “System Picker”.

How Do We Implement It?

CreateDocActivity.kt
  • Use ACTION_OPEN_DOCUMENT_TREE to select a folder: This will ask for permission in Android 10, for full access to that folder.
uig
uig
ACTION_OPEN_DOCUMENT_TREE Demo
OpenFolderActivity.kt
  • Content access using raw file paths.
  • Save image file using the MediaStore API.

The purpose of using the IS_PENDING flag in MediaStore

MediaStoreDemo.kt

When you insert an item that is marked as pending intent (value 1), by default, it will be hidden from other apps on the device.

This can be used when you use long-running downloads like video downloading from a URL. Once the download is completed, set the pending intent to 0 to reveal it to other apps to the device.

  • In the above example, we have not set/specified the path to store the image, so the OS automatically chooses the path based on the file type. Here, we took the image/JPEG, so it will store the image to the pictures folder by default.
  • You can also choose the file path with Media.RELATIVE_PATH.
  • VOLUME_EXTERNAL_PRIMARY to store in primary storage. And to get list of storages available on the phone use MediaStore.getExternalVolumeNames(context).
RelativePathDemo.kt
  • Upon getting a document URI returned, we can use it.[ContentResolver.takePersistableUriPermission] to persist the permission across restarts.
  • If your app uses scoped storage, raw file path access is limited to the app-specific directories in external storage, even if your app has been granted the READ_EXTERNAL_STORAGE permission. If your app attempts to use a raw path to open a file within external storage that doesn’t reside in the app-specific directory, a FileNotFoundException occurs. For example, the path for a file outside the app-specific directory is /sdcard/DCIM/ABC.JPG. Instead, your app should use methods in the MediaStore API.
  • In Android Q and above, it isn’t possible to modify or delete items in MediaStore directly, and explicit permission must usually be obtained to do this. The way it works is that the OS will throw a RecoverableSecurityException, which we can catch here. Inside, there’s an IntentSender which the activity can use to prompt the user to grant permission to the item so it can be either updated or deleted.
Image for post
Image for post
Scoped Storage Delete Image Demo
DeleteImageFile.kt

RequestLegacyAccess tag

In the Manifest file, we can still add that we want to use the permission access like in lower versions than Android 10. But it is only used by 2% of Android apps, and it is also going to be deprecated in the next version of Android.

This Manifest flag default value is “true” for apps targeting Android 9 (and lower).

AndroidManifest.xml

ACCESS_MEDIA_LOCATION permission

AndroidManifest.xml
  • It is runtime permission (not visible in settings).
  • No guarantees that you will always have this permission even if you have READ_EXTERNAL_STORAGE permission.
LocationAcess.kt
  • To get an exact number of bytes of files, Use MediaStore.setReqiuedOriginal(), if it isn’t a success then an exception occurs.

Modify and Delete Media Resources

  • User concerns needed while editing or deleting media resources.
  • Consent required even for file path access.
  • Bulk edit/delete in the same dialog (next Android release).

Dos and Don’ts

  1. Don’t use a static path. Lock down the file path access.
  2. Use MediaStore (recommended).
  3. MediaStore should be used properly, for example, don’t put your music files in the picture directory.
  4. Non-media files should be in the download directory (recommended).

Special App Access

  • Only apps granted by Google will have complete access to storage.
  • Submit the declaration form to Google Play.
  • Whitelisted apps by Google.

Next Release Changes

  • Update to permission UI: User will see a different permission UI based on updates and whether they’re using scoped storage or not. I.e., before, ten apps would see board access to storage and after, ten media collections have access to storage.
  • Enable file path and native libraries for reading media.
  • Updating media file and modifying APIs.
  • Protecting external app directories.
  • Enforcement to target SDK.
  • Read files not created by your app need READ_EXTERNAL_STORAGE permission.
  • To edit and delete files not contributed by your app, you need explicit user concern.
  • WRITE_EXTERNAL_STORAGE will be deprecated in the next Android release and will give read permission only when used.

Non-media file access

To access non-media files by other apps, use System Picker with SAF( Storage Access Framework). Runtime permission will be requested for complete access to that app.


Things to Remember

If Android 10 is within the scope of your application, kindly use MediaStore and System Picker for file and document access.

Happy coding!

Have a look at the scoped storage example:


Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store