Understanding the Scoped Storage in Android

With the introduction of Android 10, many new features were introduced and Scoped Storage is one of them. In this blog, we will understand how to prepare your Android apps for Scoped Storage. So, let's get started. The following is the timeline of this blog:

  • The Problem
  • The Solution - Scoped Storage
  • Key features of Scoped Storage
  • Download Collection
  • Special App Access
  • What if you don't want to use Scoped Storage?
  • Conclusion

The Problem

Before Android 10, we have a concept of Shared Storage. Every application in the device has some private storage in the internal memory and you can find this in android/data/your_package_name directory. Apart from this internal storage, the rest of the storage is called the Shared Storage i.e. every application with the storage permission can access this part of the memory. This includes media collections and other files of different applications. But the problem is that the application having the storage permission doesn't require the access of all of these files. All they want is to perform some small operation over some small part of the memory and that's it. For example, some application just needs to select a user image to upload it as the profile picture and nothing else. So, why to provide them with the full access to that Shared Storage?

Also, the second problem is that when the application is having such a wide writing capability on the storage then the files generated by the application gets scattered and when the user uninstalls the application then the files generated by the application remains in the storage only and are not deleted and takes a lot of space.

So, we need some kind of mechanism, with the help of which the apps can get the specific access they need without getting such a broad reading and writing power that they don't actually need. This can be done with the help of Scoped Storage. Let's learn about it.

The Solution - Scoped Storage

The idea of the Scoped Storage is to compartmentalize the storage into specified collections to limit the access to broad storage. There are certain principles on which the Scoped Storage is based on:

  • Better Attribution: Better attribution means the system knows which file is generated by which application. The benefit of doing this is, you will have better management of files of a particular application. Also, when you uninstall an application from the device then all the contents related to the app will also be removed unless the user explicitly wants to keep it.
  • App data protection: As we know that the internal storage of the app is private and can't be accessed by other applications. But the external storage is accessed by applications with storage permission. With the help of Scoped Storage, the data in the external storage can not be easily accessed by other applications.

So, using these principles, the Scoped Storage came with Android 10 and have some key features.

Key features of Scoped Storage

Some of the key features of Scoped Storage are:

  • Unrestricted access: Every application has unrestricted access to its own storage i.e. internal as well as external storage. So, with Android 10, you don't need to provide storage permission to write files to your own app directory on the SD card.
  • Unrestricted media: You have unrestricted access to contribute files to the media collections and downloads of your own app. So, no need to take permission if you want to save any image, video, or any other media file in media collection. You can read or write media files created by you but to read the media file of other application, you need to get the "READ_EXTERNAL_STORAGE" permission from the user. Also, the "WRITE_EXTERNAL_STORAGE" permission will be deprecated in the next Android release and you will get the read access if you used the "WRITE_EXTERNAL_STORAGE". You have to explicitly ask the user to edit the media files that are not contributed by your application.
  • Organized collection: Here, in Scoped Storage, we have an organized media collection like for images, videos, etc and downloads collection for non-media files.
  • Media location metadata: There is new permission introduced in Android 10 i.e. ACCESS_MEDIA_LOCATION. If you want to get the location of the media then you have to take this permission. For example, sometimes the picture taken by a camera also shows the location of the picture that was taken. So, if you want to show that location then you have to take this permission. It is runtime permission, so you need to declare this in your manifest file and from the MediaStore object, call the setRequireOriginal(), passing the URI of the image.
// Get location data from the ExifInterface class.
val photoUri = MediaStore.setRequireOriginal(photoUri)
contentResolver.openInputStream(photoUri).use { stream ->
    ExifInterface(stream).run {
        // If lat/long is null, fall back to the coordinates (0, 0).
        val latLong = ?: doubleArrayOf(0.0, 0.0)
    }
}
  • System Picker: In order to access other files except for these media files, we have to use System Picker which is accessed using the Storage Access Framework.
  • Read/write from outside: To read and write any files outside the collection, you need to use the System Picker.

NOTE: If you are using Scoped Storage, then you should move all your media files or all files that are present in the Shared Storage to your app's directory. Otherwise, you will lose access to those files.

Download Collection

We have seen how to edit and read media collection in Scoped Storage. For other files that are not media files like PDF or doc, Android 10 has a collection called Download Collection. Just like the Media Collection, you don't require to have any permission for editing or reading non-media files in the Download Collection of yours app.

But in Media Collection we have seen that the "READ_EXTERNAL_STORAGE" permission gives the read access to all the media files contributed by other application. While in Download Collection, to read the non-media files created by other applications, you need to use the System Picker with Storage Access Framework API. This will allow the user to explicitly provide some permission to the app. If the user grants the access then you have the full access to those files i.e. you can read, write or even delete the non-media files.

Special App Access

By using the Scoped Storage, your app will be allowed to use some limited storage. If you want to have broad access to shared storage, then your app can be given special access. So, you have to prove that your app needs full access to shared storage by submitting a declaration form to Google Play Console. After this, if your user grants the permission to have a broad access then you will get an unfiltered view of MediaStore that include non-media file. However, your app will not have access to external app directories.

What if you don't want to use Scoped Storage in Android 10?

If you are not comfortable with the Scoped Storage then you can use the previously used method of using the storage permission by using using a flag called requestLegacyExternalStorage in your manifest file.

<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>

However, in future versions of Android, this permission will not be available.

Conclusion

In this blog, we understand the concept of Scoped Storage which was introduced in Android 10. You should start using Scoped Storage ASAP becuase it is going to be compulsory in next release of Android. That's it from this blog.

Hope you learned something new today.

Have a look at our Android tutorials here.

Do share this blog with your fellow developers to spread the knowledge. You can read more blogs on Android on our blogging website.

Apply Now: MindOrks Android Online Course and Learn Advanced Android

Happy Learning :)

Team MindOrks!