Learn about Scoped Storage in Android

Tram Ho

Introduce

With the launch of Android 10, many new features were introduced and Scoped Storage was one of them. In this article, we will learn how to prepare your Android applications for Scoped Storage. So let’s get started. Following is the timeline of this blog:

  • Problem
  • Solution – Scoped Storage
  • Key features of Scoped Storage
  • Download Collection
  • Access special application
  • What if I do not want to use Scoped Storage?
  • Conclude

Reference source: https://blog.mindorks.com/understanding-the-scoped-storage-in-android

Problem

Before Android 10, we had the concept of Shared Storage . Every app in your device has some internal storage, and you can find it in the android / data / your_package_name directory . In addition to this internal memory, the remainder of the memory is called Shared Storage , which means that every application with storage permissions can access this part of the memory. This includes media collections and other files of different applications. But the problem is that the application with storage permissions does not require access to all these files. All they want is to do some small operations on a small portion of memory and that’s the problem. For example, some apps just need to select a user image to upload it as a profile picture and nothing else. So, why give them full access to that shared storage?

In addition, the second problem is that when the application has such wide write capability on the storage, the files created by the application are fragmented and when the user uninstalls the application, the files created by the application only remain in storage and not deleted and take up a lot of space.

So we need some kind of mechanism, with the help that applications can have the specific access they need without having the ability to read and write so widely that they don’t really need it. This can be done with the help of Scoped Storage . Let’s find out about it.

Solution – Scoped Storage

The idea of ​​Scoped Storage is to split storage into designated collections to limit access to wide storage. There are certain principles that Scoped Storage relies on:

  • Better Attribution : Better allocation means the system knows which files are created by which applications. The benefit of this is, you will better manage the files of a specific application. In addition, when you uninstall an application from the device, all content related to the application will also be deleted unless the user explicitly wants to keep it.
  • App data protection : As we know that the internal memory of the application is private and inaccessible to other applications. But external memory is accessed by apps that have storage permissions. With the help of Scoped Storage , data in external memory cannot be easily accessed by other applications.

So using these guidelines, Scoped Storage comes with Android 10 and has a number of 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, that is, internal as well as external storage. So with Android 10, you don’t need to provide storage permission to write files to your own apps folder on the SD card.
  • Unrestricted media : You have unlimited access to contribute files to media collection and download your own applications. Therefore, there is no need to ask permission if you want to save any images, videos or any other media files in the media collection. You can read or write media files created by you but to read media files of other applications, you need READEXTERNALSTORAGE permission from users. Also, WRITE_EXTERNAL_STORAGE permission will not be accepted in the next Android release and you will have read access if you use WRITE_EXTERNAL_STORAGE . You must explicitly ask users to edit media files not contributed by your application.
  • Organized collection : Here, in Scoped Storage , we have an organized media collection like for images, videos, etc. and download collections for non-media files.
  • Media location metadata : Has a new permission introduced in Android 10, ie ACCESS_MEDIA_LOCATION . If you want to get the location of the media then you must have this permission. For example, sometimes images taken by the camera also show the location of the images taken. So if you want to display that location then you must have this permission. It’s runtime right, so you need to declare this in your manifest and from the MediaStore object, call setRequireOrigen () , passing the image’s URI.

  • System Picker : To access files other than these media files, we must use the System Picker accessed by the Storage Access Framework .
  • Read / write from outside : To read and write any files outside the collection, you need to use System Picker .

NOTE: If you are using Scoped Storage, you should move all media files or all files that are in Shared Storage to the application’s folder. Otherwise, you will lose access to those files.

Download Collection

We saw how to edit and read media collection in Scoped Storage. For other non-media files like PDF or doc, Android 10 has a collection called Download Collection . Like Media Collection , you don’t need any rights to edit or read non-media files in your app’s Download Collection.

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

Access special application

By using Scoped Storage, your application will be allowed to use a limited amount of memory. If you want broad access to shared memory, your application may be granted special access. Therefore, you must prove that your application needs full access to shared memory by submitting a declaration form to Google Play Console. Later, if your users grant access to broad access, you will get MediaStore’s unfiltered view including non-media files. However, your application will not have access to external application directories.

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

If you are dissatisfied with Scoped Storage, you can use the method used previously to use storage rights by using a flag named requestLegacyExternalStorage in your manifest file.

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

Conclusion

Thus, in this article, we understand the concept of Scoped Storage was introduced in Android 10. You should start using Scoped Storage ASAP because it will be required in the next version of Android.

Share the news now

Source : Viblo