- Home
- >
- Ecommerce Development
- >
- Update Scoped Storage in Android 10- All You Need to Know
Scoped Storage in Android 10- All You Need to Know is an article sent to you by the InApps editorial team. Hope readers will have more useful knowledge at www.inapps.net
You are viewing the article: 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
– 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.
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.
• Use ACTION_OPEN_DOCUMENT_TREE to select a folder: This will ask for permission in Android 10, for full access to that folder.
• Saving Image file using MediaStore API.
– Purpose of using IS_PENDING flag in MediaStore?
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).
• 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.
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.
Follow this to make sure you’ve got Scoped Storage in Android 10- All You Need to Know. Save and share with those around you these extras.
To learn more about ECOMMERCE DEVELOPMENT
Contact us:
www.inapps.net
Let’s create the next big thing together!
Coming together is a beginning. Keeping together is progress. Working together is success.