Monday 30 December 2019

Scoped Storage in Android

In Android 10 (SDK version 29) many new features were introduced and Scoped Storage is one of them. 

The Problem, Why scoped storage introduced in Android Q?

  • 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. 

1st Problem

  • application having the storage permission doesn't require the access of all of these files always
  • For example,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?

2nd problem 

  • application is having such a wide writing capability on the storage then the files generated by the application gets scattered 
  • when the user uninstalls the application then the files generated by the application remains in the storage and 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.

The Solution - Scoped Storage
Better Attribution: 

  • Better attribution means the system knows which file is generated by which application
  • when user 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: 

  • 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

Key features of Scoped Storage
Unrestricted access: 
  • Every application has unrestricted access to its own storage( internal as well as external)
  • you don't need to provide storage permission to write files to your own app directory on the SD card.
Unrestricted media: 
  • unrestricted access to contribute files to the media collections and downloads of your own app.
  • 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
  • To read the media file of other application, you need to get the "READ_EXTERNAL_STORAGE" permission from the user. 
Organized collection: 
  • organized media collection like for images, videos, etc and downloads collection for non-media files.
  • There is new permission introduced in Android 10 i.e. ACCESS_MEDIA_LOCATION( get the location of the media )
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.
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.

How to access simple files(using System Picker)?
  • In order to access files on the device, you can use the Storage Access Framework (SAF).
  • By using ACTION_OPEN_DOCUMENT a dialog will be shown to the user where the needed documents can be selected. 
  • There’s also ACTION_OPEN_DOCUMENT_TREE to ask the user to select a directory and
  • ACTION_CREATE_DOCUMENT in order to save files.
Sample Application 
I am creating a sample application to demonstrate scope permission.  You can find it in my GitHub account 
screen shot


Thanks
Saurabh
Happy Coding !!!


No comments:

Post a Comment

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...