The Android Developer Challenge is back! Submit your idea before December 2.

Save files on device storage

Android uses a file system that's similar to disk-based file systems on other platforms. This page describes how to work with the Android file system to read and write files with the File APIs.

A File object works well for reading or writing large amounts of data in start-to-finish order without skipping around. For example, it's good for image files or anything exchanged over a network.

The exact location of the where your files can be saved might vary across devices, so you should use the methods described on this page to access internal and external storage paths instead of using absolute file paths.

To view files on a device, you can log the file location provided by methods such as File.getAbsolutePath(), and then browse the device files with Android Studio's Device File Explorer.

Choose internal or external storage

All Android devices have two file storage areas: internal storage and external storage. These names come from the early days of Android, when most devices offered built-in non-volatile memory (internal storage), plus a removable storage medium such as a micro SD card (external storage). Many devices now divide the permanent storage space into separate "internal" and "external" partitions. So even without a removable storage medium, these two storage spaces always exist, and the API behavior is the same regardless of whether the external storage is removable.

Because the external storage might be removable, there are some differences between these two options as follows.

Internal storage:

  • It's always available.
  • Files saved here are accessible by only your app.
  • When the user uninstalls your app, the system removes all your app's files from internal storage.

Internal storage is best when you want to be sure that neither the user nor other apps can access your files.

External storage:

  • It's not always available, because the user can mount the external storage as USB storage and in some cases remove it from the device.
  • It's world-readable, so files saved here may be read outside of your control.
  • When the user uninstalls your app, the system removes your app's files from here only if you save them in the directory from getExternalFilesDir().

External storage is the best place for files that don't require access restrictions and for files that you want to share with other apps or allow the user to access with a computer.

Note: Although apps are installed onto the internal storage by default, you can allow your app to be installed on external storage by specifying the android:installLocation attribute in your manifest. Users appreciate this option when the APK size is very large and they have an external storage space that's larger than the internal storage. For more information, see App Install Location.

Additional resources

For more information about saving files to the device's storage, consult the following resources.