Android uses a file system that's similar to disk-based file systems on other platforms. The system provides several options for you to save your app data:
- App-specific storage: Store files that are meant for your app's use only, either in dedicated directories within an internal storage volume or different dedicated directories within external storage. Use the directories within internal storage to save sensitive information that other apps shouldn't access.
- Shared storage: Store files that your app intends to share with other apps, including media, documents, and other files.
- Preferences: Store private, primitive data in key-value pairs.
- Databases: Store structured data in a private database using the Room persistence library.
The characteristics of these options are summarized in the following table:
|Type of content||Access method||Permissions needed||Can other apps access?||Files removed on app uninstall?|
|App-specific files||Files meant for your app's use only||From internal storage,
From external storage,
|Never needed for internal storage
Not needed for external storage when your app is used on devices that run Android 4.4 (API level 19) or higher
|No, if files are in a directory within internal storage
Yes, if files are in a directory within external storage
|Media||Shareable media files (images, audio files, videos)||
Permissions are required for all files on Android 9 (API level 28) or lower
|Yes, though the other app needs the
|Documents and other files||Other types of shareable content, including downloaded files||Storage Access Framework||None||Yes, through the system file picker||No|
|App preferences||Key-value pairs||Jetpack Preferences library||None||No||Yes|
|Database||Structured data||Room persistence library||None||No||Yes|
The solution you choose depends on your specific needs:
- How much space does your data require?
- Internal storage has limited space for app-specific data. Use other types of storage if you need to save a substantial amount of data.
- How reliable does data access need to be?
- If your app's basic functionality requires certain data, such as when your app is starting up, place the data within internal storage directory or a database. App-specific files that are stored in external storage aren't always accessible because some devices allow users to remove a physical device that corresponds to external storage.
- What kind of data do you need to store?
- If you have data that's only meaningful for your app, use app-specific storage. For shareable media content, use shared storage so that other apps can access the content. For structured data, use either preferences (for key-value data) or a database (for data that contains more than 2 columns).
- Should the data be private to your app?
- When storing sensitive data—data that shouldn't be accessible from any other app—use internal storage, preferences, or a database. Internal storage has the added benefit of the data being hidden from users.
Categories of storage locations
Android provides two types of physical storage locations: internal storage and external storage. On most devices, internal storage is smaller than external storage. However, internal storage is always available on all devices, making it a more reliable place to put data on which your app depends.
Removable volumes, such as an SD card, appear in the file system as part of
external storage. Android represents these devices using a path, such as
Apps themselves are stored within internal storage by default. If your APK size is very large, however, you can indicate a preference within your app's manifest file to install your app on external storage instead:
<manifest ... android:installLocation="preferExternal"> ... </manifest>
Permissions and access to external storage
On earlier versions of Android, apps needed to declare these permissions to access any file outside the app-specific directories on external storage. More recent versions of Android rely more on a file's purpose than its location for determining an app's ability to access that file. This purpose-based storage model improves user privacy because apps are given access only to the areas of the device's file system that they actually use.
To give users more control over their files and to limit file clutter, apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. Such apps have access only to the app-specific directory on external storage, as well as specific types of media that the app has created.
Use scoped storage unless your app needs access to a file that's stored outside
of an app-specific directory and outside
of a directory that the
APIs can access. If you store app-specific files on external storage, you can
make it easier to adopt scoped storage by placing these files in an
app-specific directory on external
storage. That way, your app
maintains access to these files when scoped storage is enabled.
Best practices for operating on files
This section presents a few general best practices for opening and sharing files from your app.
Don't open and close files repeatedly
To help maintain your app's performance, don't open and close the same file multiple times. It's expensive for the system to open a file and to read a file for the first time.
Share individual files
If you want to share individual files or app data with other apps, Android provides the following APIs:
- If you want to share specific files with other
apps, use the
- If you want to expose data to other apps, you can use a content provider. Content providers give you full control of what read-and-write access is available to other apps. Although you can use content providers with any storage medium, they're most commonly used with databases.
View files on a device
To view the files stored on a device, use Android Studio's Device File Explorer.
For more information about data storage, consult the following resources.