Everything about storage on Android
Understanding Storage on Android
Overview of Android Storage Architecture
- Yacine Rezgui introduces the topic of storage on Android, focusing on APIs and best practices.
- The primary storage volume is where the OS and apps are located; it includes internal storage that is private to each app for confidential data like databases.
- External storage allows for app-specific files but can be limited by device capacity; sensitive information should not be stored here due to accessibility by other apps before Android 11.
- Shared storage is accessible by all apps, allowing users to see files in file managers and galleries; files remain after app uninstallation.
- MediaStore indexes shared storage content, providing efficient access to media file metadata.
Scoped Storage Changes
- Scoped storage was introduced in Android 10, restricting access to shared files based on file type (e.g., only media files without permissions).
- WRITE_EXTERNAL_STORAGE permission is deprecated; user consent is now required for modifying or deleting media files.
- Location metadata for media files is removed automatically; developers must request access separately if needed.
Common Use Cases for Storage
Downloading User Data
- For downloading confidential user data, OkHttp library is used to create HTTPS requests and save responses securely in internal storage.
- When downloading large non-confidential assets, check available space first in internal storage before using external options if necessary.
Adding Files to Shared Storage
- To add images or videos to shared storage, permissions were previously required up until Android 9; this changed with scoped storage from Android 10 onwards.
- Use environment constants (like DIRECTORY_PICTURES or DIRECTORY_MOVIES) when saving different types of files into appropriate folders within shared storage.
Scanning Files into MediaStore
Enhancements in File Access and Media Selection on Android
Utilizing the OpenDocument Intent
- The document picker is accessed via the action OpenDocument intent, requiring the addition of Jetpack Activity dependency to the project.
- Registration of the OpenDocument activity result is necessary to handle intent resolution logic for file selection.
- Upon user selection, a URI can be obtained to open files using the content resolver, specifically filtering for PDF MIME types.
- The action OpenDocument intent is compatible with devices running Android 4.4 (KitKat) and higher.
Transitioning from External Storage Permissions
- Prior to Android 13, accessing shared storage required read external storage permissions and custom UI implementations for item selection.
- A simpler and more privacy-friendly approach was sought for developers and users alike.
Introduction of Photo Picker in Android 13
- Android 13 introduced a photo picker that enhances user experience by allowing image and video selection without runtime permissions.
- This feature is maintenance-free, enabling developers to limit selectable items with minimal code integration; it’s backported to Android 11 and 12.
- Upcoming support for cloud media providers like Google Photos will allow access to backed-up photos/videos not stored locally on devices.
Implementing Visual Media Selection
- To utilize the photo picker, add Jetpack Activity dependency version 1.6 or higher; register PickVisualMedia activity result for handling intents.
- The implementation allows fallback to document picker on older devices while providing URIs for selected files through content resolver access.
New Permissions and Privacy Improvements in Android 13
- For persistent file access post-app closure, developers can use takePersistableUriPermission; this is useful for applications like gallery backups.
- New permissions targeting API level 33 provide specific access rights for media files including images, videos, or audio.