InApps Technology
Scoped storage in android 10

Update Scoped Storage in Android 10- All You Need to Know

Anh Hoang June 17, 2022 10 min read

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.

Key Summary

This article from InApps Technology, authored by Phu Nguyen, explores Scoped Storage, a new storage system introduced in Android 10 to enhance privacy, security, and storage management compared to the traditional Shared Storage model. It outlines its features, implementation, and implications for Android app development. Key points include:

  • Shared Storage (Pre-Android 10): Apps had private directories in internal storage (android/data/package_name), inaccessible to other apps. Broad storage permissions were often required for simple tasks (e.g., downloading images), leading to residual files after app uninstallation, causing storage issues.
  • Apps had private directories in internal storage (android/data/package_name), inaccessible to other apps.
  • Broad storage permissions were often required for simple tasks (e.g., downloading images), leading to residual files after app uninstallation, causing storage issues.
  • Scoped Storage Overview: Definition: A system that organizes files into separate Collections, restricting broad access to the entire storage. Benefits: Better Attribution: Tracks which app created a file, enabling complete data removal upon uninstallation. Protecting App Data: App-specific directories (internal/external) are private. User Data Privacy: Prevents downloaded files (e.g., images) from being accessed by other apps.
  • Definition: A system that organizes files into separate Collections, restricting broad access to the entire storage.
  • Benefits: Better Attribution: Tracks which app created a file, enabling complete data removal upon uninstallation. Protecting App Data: App-specific directories (internal/external) are private. User Data Privacy: Prevents downloaded files (e.g., images) from being accessed by other apps.
  • Better Attribution: Tracks which app created a file, enabling complete data removal upon uninstallation.
  • Protecting App Data: App-specific directories (internal/external) are private.
  • User Data Privacy: Prevents downloaded files (e.g., images) from being accessed by other apps.
  • Key Features: Unrestricted Access: Apps can freely access their own internal/external storage and Media Collections (e.g., images, videos) without permissions. Restricted Access: Reading Media Collections requires READ_EXTERNAL_STORAGE; non-media files (e.g., PDFs) use the System Picker. Location Metadata: ACCESS_MEDIA_LOCATION permission allows access to image location data (latitude/longitude) via EXIFInterface. Pending Intent Flag: Files marked with IS_PENDING=1 are hidden during long-running downloads (e.g., videos) until set to 0 for visibility. Path Selection: Use MediaStore.RELATIVE_PATH to specify storage paths (e.g., “MYPICS” for images); defaults to type-based folders (e.g., Pictures for image/jpeg). Storage Options: VOLUME_EXTERNAL_PRIMARY targets primary storage; MediaStore.getExternalVolumeNames lists available storage.
  • Unrestricted Access: Apps can freely access their own internal/external storage and Media Collections (e.g., images, videos) without permissions.
  • Restricted Access: Reading Media Collections requires READ_EXTERNAL_STORAGE; non-media files (e.g., PDFs) use the System Picker.
  • Location Metadata: ACCESS_MEDIA_LOCATION permission allows access to image location data (latitude/longitude) via EXIFInterface.
  • Pending Intent Flag: Files marked with IS_PENDING=1 are hidden during long-running downloads (e.g., videos) until set to 0 for visibility.
  • Path Selection: Use MediaStore.RELATIVE_PATH to specify storage paths (e.g., “MYPICS” for images); defaults to type-based folders (e.g., Pictures for image/jpeg).
  • Storage Options: VOLUME_EXTERNAL_PRIMARY targets primary storage; MediaStore.getExternalVolumeNames lists available storage.
  • New Flags and Permissions: RequestLegacyAccess: Allows legacy storage access in the manifest (default true for Android 9 and below, false for Android 10). Used by only 2% of apps and set for deprecation. ACCESS_MEDIA_LOCATION: Runtime permission for image location data, not visible in settings, and not guaranteed even with READ_EXTERNAL_STORAGE. MediaStore.setRequiredOriginal(): Retrieves exact file byte size, throwing an exception if unsuccessful.
  • RequestLegacyAccess: Allows legacy storage access in the manifest (default true for Android 9 and below, false for Android 10). Used by only 2% of apps and set for deprecation.
  • ACCESS_MEDIA_LOCATION: Runtime permission for image location data, not visible in settings, and not guaranteed even with READ_EXTERNAL_STORAGE.
  • MediaStore.setRequiredOriginal(): Retrieves exact file byte size, throwing an exception if unsuccessful.
  • Do’s and Don’ts: Avoid: Static file paths (restricted in Android 10) and placing files in incorrect directories (e.g., music in Pictures). Recommended: Use MediaStore for media files and System Picker for non-media files (e.g., Downloads directory). Modifying/Deleting Media: Requires user consent via a dialog (bulk edit/delete dialog in future releases).
  • Avoid: Static file paths (restricted in Android 10) and placing files in incorrect directories (e.g., music in Pictures).
  • Recommended: Use MediaStore for media files and System Picker for non-media files (e.g., Downloads directory).
  • Modifying/Deleting Media: Requires user consent via a dialog (bulk edit/delete dialog in future releases).
  • Special App Access: Apps needing broad storage access must submit a declaration to Google Play for whitelisting; no access to external app directories.
  • Apps needing broad storage access must submit a declaration to Google Play for whitelisting; no access to external app directories.
  • Use Cases: Media Player: Uses MediaStore and Content Resolver APIs with READ_EXTERNAL_STORAGE to access video files. Image Editing/Deletion: Requires MediaStore API and READ_EXTERNAL_STORAGE; bulk deletion dialog in next release. File Attachment (e.g., Gmail): Uses Storage Access Framework (SAF) with ACTION_CREATE_DOCUMENT, no permissions needed.
  • Media Player: Uses MediaStore and Content Resolver APIs with READ_EXTERNAL_STORAGE to access video files.
  • Image Editing/Deletion: Requires MediaStore API and READ_EXTERNAL_STORAGE; bulk deletion dialog in next release.
  • File Attachment (e.g., Gmail): Uses Storage Access Framework (SAF) with ACTION_CREATE_DOCUMENT, no permissions needed.
  • Implementation: ACTION_OPEN_DOCUMENT: Selects individual files. ACTION_OPEN_DOCUMENT_TREE: Grants full folder access with user permission. MediaStore API: Saves files (e.g., images) with IS_PENDING for temporary isolation; use ContentResolver.takePersistableUriPermission for persistent access across restarts. Error Handling: Accessing non-app-specific files outside app directories throws FileNotFoundException; modifying/deleting media may throw RecoverableSecurityException, requiring user consent via IntentSender.
  • ACTION_OPEN_DOCUMENT: Selects individual files.
  • ACTION_OPEN_DOCUMENT_TREE: Grants full folder access with user permission.
  • MediaStore API: Saves files (e.g., images) with IS_PENDING for temporary isolation; use ContentResolver.takePersistableUriPermission for persistent access across restarts.
  • Error Handling: Accessing non-app-specific files outside app directories throws FileNotFoundException; modifying/deleting media may throw RecoverableSecurityException, requiring user consent via IntentSender.
  • Expected Changes in Next Release: Updated permission UI distinguishing between broad storage access and media collection access. Enhanced file path and native library support for media reading. Improved APIs for updating media files and protecting external app directories. Stricter enforcement of target SDK requirements. WRITE_EXTERNAL_STORAGE deprecation, granting read-only access. Non-media file access via SAF and System Picker with runtime permissions.
  • Updated permission UI distinguishing between broad storage access and media collection access.
  • Enhanced file path and native library support for media reading.
  • Improved APIs for updating media files and protecting external app directories.
  • Stricter enforcement of target SDK requirements.
  • WRITE_EXTERNAL_STORAGE deprecation, granting read-only access.
  • Non-media file access via SAF and System Picker with runtime permissions.
  • InApps Insight: InApps Technology leverages expertise in Android app development, integrating Scoped Storage, React Native, ReactJS, Microsoft’s Power Platform, and Azure, using Power Fx for low-code solutions and Azure Durable Functions for scalable workflows. Combines Node.js, Vue.js, GraphQL APIs (e.g., Apollo), and Azure to deliver privacy-focused, efficient mobile solutions, targeting startups and enterprises with Millennial-driven expectations.
  • InApps Technology leverages expertise in Android app development, integrating Scoped Storage, React Native, ReactJS, Microsoft’s Power Platform, and Azure, using Power Fx for low-code solutions and Azure Durable Functions for scalable workflows.
  • Combines Node.js, Vue.js, GraphQL APIs (e.g., Apollo), and Azure to deliver privacy-focused, efficient mobile solutions, targeting startups and enterprises with Millennial-driven expectations.
  • Call to Action: Contact InApps for guidance on implementing Scoped Storage or developing eCommerce and mobile applications.
  • Contact InApps for guidance on implementing Scoped Storage or developing eCommerce and mobile applications.

Update Top 4 expectation mismatch while outsourcing an IT project

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

Update Cross Platform Technologies to Deal with Multi-Platform Reality

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.

Update 4 Must Have Features on Wearable Device Apps

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

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

Rate this post

ShareLinkedInX

Want to apply these insights?

Our AI architects offer free 45-minute consultations to discuss your specific use case.

Book a Discovery Call