Skip to content

Android Storage

Updated: 05-05-2021

New apps and app updates must target Android 10 (API level 29) or higher; except for Wear OS apps, which must target API level 28.

Starting in August 2021, new apps will need to:

  • Publish with the Android App Bundle format.
  • Use Play Asset Delivery or Play Feature Delivery to deliver assets or features that exceed download size of 150MB. Expansion files (OBBs) will no longer be supported for new apps.
  • Target API level 30 (Android 11) or above and adjust for behavioral changes; except Wear OS apps, which must continue to target API level 28.

Starting in November 2021, app updates will be required to target API level 30 or above and adjust for behavioral changes in Android 11.

Notes

Use:

  • Context.getExternalFilesDir()
  • Context.getExternalCacheDir()
  • Context.getExternalMediaDirs()

Replaces Environment.getExternalStorageDirectory().

Android 9 August 2018

  • Internal storage is private to your app, and other apps will never be able to access data there.
  • Shared storage can be accessed when granted READ/WRITE_EXTERNAL_STORAGE
  • This includes all locations in /sdcard/ and gives apps the ability to query MediaStore.
  • App attributed directories do not need the storage permission.

Android 10 July 2020 - Scoped Storage

  • All apps can contribute to MediaStore without any permission, however to view content contributed by other apps, READ_EXTERNAL_STORAGE must be obtained.
  • Files outside the MediaStore can be accessed by the system picker.
  • Internal storage is left, ie Context.getDataDir().

Introduced in Android 10

  • Unrestricted access to your own app storage.
  • Unrestricted media and downloads contributions (Pictures, Music, Video, Downloads)
    • Apps can contribute to these collections without any permissions.
    • To save in Downloads, the user must give explicit access via the File Picker.
  • Runtime permission only gives read access to media
  • User confirmation required for modifying media (delete non app file/dir)
  • Location metadata gated by new permission

Android 11 September 8, 2020

Storage access with Android 11 Youtube Video

  • Enabled File Paths APIs
  • Bulk media modifications APIs
  • All Files Access < broadly read file system
  • Private app storage

Using File Paths

  • Allow devs to use file apis while keeping the new security features.
    • “FSE” File System - User Space; re-enabled File API with scoped storage
    • Under the hoof file access is delegated tot he MediaStore API
    • File path API is a convenience API to the MediaStore
  • Alternative to MediaStore APIs
  • Better compatability
  • Java Files API
  • Native Libraries (C, C++)
  • Same scoped storage access
  • Immediately indexed by MediaStore (old way you had to scan the file)
  • MediaStore holds file path in ‘data’ column

Modifying Media

  • Can batch modify after confirmation from user
  • createWriteRquest()
  • createReadRequest()
  • createTrashRequest() (gives the user a chance to recover file later), deleted by OS after 30 days.
    • Can be un-trashed by the file owner or user consent before 30 days
  • createFavoriteRequest()
    • works across apps

All Files Access

  • MANAGE_EXTERNAL_STORAGE
  • Write access to all shared files.
  • Special app access.
  • Google Play manual review.

Tips on Migrating

  • Legacy storage flag. (all files access/file paths)
    • To ensure correct behavior for users of Android 10
  • Storage access framework
    • Custom picking experience for custom files like documents. (does not require permissions)
  • Modifying media.
    • modifying media files you do not own, requires user consent (MediaStore consent APIs)
  • Top level directories (don’t save in custom top level directories)
    • requires the SAF which “is not the best experience for users”
    • App uninstalled and reinstalled, the app would loose access to the files
    • Use organized media collections or private storage instead
  • Content Providers
    • share data with other apps
    • most secure way to share files

Google Dev

Random

Youtube

Misc

This is older outdated stuff.

Internal Storage

is storage that is not accessible by the user, except via installed apps (or by rooting their device). Example: data/data/app_packageName

Primary External Storage: In built shared storage which is “accessible by the user by plugging in a USB cable and mounting it as a drive on a host computer”. Example: When we say Nexus 5 32 GB.

Secondary External Storage

Removable storage. Example: SD Card.

  • getExternalFilesDir()
    • Returns the path to files folder inside Android/data/data/your_package/ on primary external storage.
  • getExternalStorageDirectory()
    • Returns the root path to your secondary external storage directory (SD card e.g mnt/sdcard/). If you save data on this path and uninstall the app, that data won’t be lost.

Audio Evolution

What is completely ignored in the video and documentation is that you can just write to the Documents folder (on > internal storage, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath()) using the JAVA File and NDK FILE API’s. No SAF needed! The files will also stay there on uninstallation. For external SD cards, things remain as they were since Android 4.4: when storing in the app-specific directory, files will be removed on app-uninstall. But for internal storage, we can just move our files to Documents and be done.