Scoped Storage in Android 10- All You Need to Know

Mar 2nd, 2020

Scoped Storage in Android 10- All You Need to Know

Every Android version has something for developers and users alike. The Android 10 version has many user-friendly features and performance enhancements. For example, Android has introduced a new storage system in Android 10 known as Scoped Storage. But before diving deep into this new storage system and discussing its role in Android app development, let’s go through the aspects of a current Shared Storage system.

Brief on Shared Storage:

Before the advent of Scoped Storage, the shared storage had the following characteristics for Android phone users-
• All apps have their private Directory in Internal Storage i.e android/data/your package name, which is not visible to other apps.
• Most of the apps require broad storage permission to perform simple functions. For example, downloading images or as image picker, etc. Now, at the time of uninstalling apps, most of the app-related files don’t get deleted. This results in the issue of insufficient storage.
Android 10 has brought a redesigned storage system to overcome these issues. We know it as Scoped Storage.

What is Scoped Storage?

• It’s a concept of storing files, images, etc separately called Collections which restricts the conventional access of the whole storage.
• Better Attribution: This means which app created which file is known by the system. 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: Downloaded image not to 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 Collection: E.g Save image file without Permission.
3. Only Media Collections can be read with Storage Permission.
4. Location MetaData ACCESS_MEDIA_LOCATION for the location of the image.
5. For files like pdf, text, etc use System Picker.
6. Reading and writing outside of collection requires the “System Picker”.

Some New Flags

scoped-storage-2

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 a long-running Download, eg. Video Downloading from URL.

Once the download is completed, set the pending intent to 0, to reveal it to other apps to the device

values.put(MediaStore.Images.Media.IS_PENDING,0)
resolver.update(item,null,null,null)

In the above example, we have not set/specified the path to store the image, so OS automatically chooses the path based on the file type. Here we took image/jpeg, So it will store images to the Pictures folder by default.
You can also choose the File path by Media.RELATIVE_PATH

Eg.
val values = ContentValues().apply{
put(MediaStore.Images.Media.RELATIVE_PATH,”MYPICS”)
put(MediaStore.Images.Media.DISPLAY_NAME,”IMG.JPG”)
put(MediaStore.Images.Media.MIME_TYPE,”image/jpeg”)
put(MediaStore.Images.Media.IS_PENDING,1) }

VOLUME_EXTERNAL_PRIMARY to store in primary Storage.

RequestLegacyAccess Tag

In the manifest file, we can still add to use the permission access like in the lower version than Android 10. But only used by 2% of an android app, and also going to be deprecated in the next version of Android.

The manifest flag default value is true for apps designed to run on Android 9 (and lower versions).
The default value is false for apps developed for Android 10.

scopedstorage-2

Scoped Storage

ACCESS_MEDIA_LOCATION permission

It is Runtime permission
Used to get Location of image ie. Latitude and Longitude with EXIFInterface.
Not visible in settings
No guarantee you will have always have this permission, also if you have READ_EXTERNAL_STORAGE permission
To get the exact number of bytes of Files, Use MediaStore.setRequiredOriginal() , if not success the exception occurs

Do/Don’ts For Storage

Don’t Use a Static path. Locked down the file path access.
Use MediaStore (recommend).
Media Store should be used in a proper way, for example, Don’t put your Music files in Picture directory.
Non-media files should be in the Download directory(recommend).

To Modify and Delete Media

User Consent required when edit or delete media
Consent required even for file path access
Bulk edit delete in same dialog(Next Release)

Special APP Access

Only granted app that can prove the clear need to storage
Submit declaration form to Google Play
WhiteListed apps by Google
No access to external app directories

Examples(Use Cases):

1. Media Player App
Read all video files
Mediastore API
Content Resolver API
READ_EXTERNAL_STORAGE required

2. Edit Image/Delete Image
Media Store API
READ_EXTERNAL_STORAGE
Next Release -Bulk Delete in single dialog available

3. GMAIL file attach
SAF
NO Permission required
UI is handled by intent
To save file while sending you can use ACTION_CRATE_DOCUMENT

How to Implement?

• Use ACTION_OPEN_DOCUMENT to select File.

scoped-storage-1

• Use ACTION_OPEN_DOCUMENT_TREE to select a folder: This will ask for permission in Android 10, for full access to that folder.

Scoped-Storage3

Scoped-Storage-4

• Saving Image file using MediaStore API.

Purpose of using IS_PENDING flag in MediaStore?

scopedstorage-5

When you insert an item which 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 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 OS automatically chooses the path based on file type. Here we took image/jpeg, So it will store images to the Pictures folder by default.

• You can also choose the file path by 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).

Scoped Storage

• Upon getting a document URI returned, we can use it.[ContentResolver.takePersistableUriPermission] in order to persist the permission across restarts.

• If your app uses scoped storage, raw file path access is limited to the app-specific directories on 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, but it doesn’t reside in the app-specific directory, then an exception called FileNotFoundException occurs. For example, the path for a file outside the app-specific directory is /sdcard/DCIM/ABC.JPG. Here, your app should use methods given 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. Here, the OS will throw a RecoverableSecurityException that we can catch here. Inside, we have an IntentSender that can be used by an activity to prompt the user to grant permission to the item update or deletion.

scopedstorage-6

Android Q

Expected Changes in the Next Release

Permission UI Update: User will see a different Permission UI either based on update and Scoped Storage or not i.e. before, 10 apps would see the Board Access to storage and subsequent 10 apps will see media collection access to storage.

Enable File path and native libraries for reading media.

Updating Media files and modifying APIS.

Protecting External app Directories.

Enforcement to target SDK.

Read files not created by your app need READ_EXTRNAL_STORAGE Permission.

To edit and delete files not contributed by your app, then 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 have been asked for complete access to that app.

Things To Remember: If Android 10 is within the scope of your application, kindly use MediaStore/SystemPicker for File and Documents access.

Android App Development CTA

Comments are closed.

Let's Discuss Your Project

Get free consultation and let us know your project idea to turn
it into an amazing digital product.

Let’s talk

NEWS & BLOG

Related Blogs

How Hiring Dedicated Android App Developers Helps Your Company

Android Apps Sep 2nd, 2020

How Hiring Dedicated Android App Developers Helps Your ...

Read more
Top Features of Android 11 from Developer’s Point of View

Android Apps Mar 4th, 2020

Top Features of Android 11 from Developer’s Point of ...

Read more
New Features and Improvements- Android Studio 3.5

Android Apps Oct 24th, 2019

New Features and Improvements- Android Studio 3.5...

Read more

INQUIRY

Let's get in touch

UNITED STATES

31236 Meadowview Square,
Delmar, DE 19940, USA

Sales: +1 667 771 6758

UNITED KINGDOM

13 Layton Road, Hounslow,
London, TW3 1YJ

Sales: +44 7404 607567

INDIA

2nd Floor, Sun Avenue One, Bhudarpura, Ayojan Nagar, Nr. Shyamal Cross Road, Ahmedabad, Gujarat-380006

Sales: +91 635-261-6164

For Project Inquiries

biolah

depo 25 bonus 25 to 5x

depo 25 bonus 25

depo 25 bonus 25

mndrmndr.com

bonusdeposit.net

https://www.greentourstanzania.com/wp-includes/customize/

https://temp1.novotest.biz/id/

depo 25 bonus 25

https://sumberjo-blitar.desa.id/images

https://sumberjo-blitar.desa.id/data

depo 25 bonus 25 to 5x

depo 25 bonus 25

https://www.greentourstanzania.com/wp-includes/js/product/

https://smpabbs.sch.id/gacor/100/

https://smpabbs.sch.id/gacor/bonus/

deposit 25 bonus 25

depo 25 bonus 25

bonus new member 100

https://ppdb.smk-kosgoro.sch.id/data/depo 25 bonus 25https://jesus.nouvellevie.com/wp-includes/images/Getoko.iddepo 25 bonus 25https://bonus-baru.s3.ap-southeast-1.amazonaws.com/link-daftar-slot-gacor.htmlhttps://bonus-baru.s3.ap-southeast-1.amazonaws.com/scatter-pink-paling-gacor.htmlhttps://worldlisteningproject.org/wp-includes/depo25bonus25/bonus new member 100depo 25 bonus 25