File system

Android provides the locations for storing app-specific files.

The system prevents other apps from accessing these locations, and on Android 10+ these locations are encrypted in internal storage directories.

Before writing app-specific files to internal storage, your app should query the free space on the device.

Another app can access app-specific files in external storage directories if that app has the proper permissions. If you specifically intend to create files that other apps should be able to access, your app should store these files in the shared storage

The Environment class provides access to environment variables and methods related to the file system.

You can get app-specific directories from context object such as an Activity using the methods listed below.

method description
getCacheDir() Returns the absolute path to the application specific cache directory on the filesystem.
getDataDir() Returns the absolute path to the directory on the filesystem where all private files belonging to this app are stored.
getDir(name, mode) Retrieve, creating if needed, a new directory in which the application can place its own custom data files.
getFilesDir() Returns the absolute path to the directory on the filesystem where files created with openFileOutput() are stored.
getExternalCacheDir() Returns absolute path to application-specific directory on the primary shared/external storage device where the application can place cache files it owns.
getExternalCacheDirs() Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place cache files it owns.
getExternalFilesDir(type)

Returns the absolute path to the directory on the primary shared/external storage device where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.

The type parameter may be null for the root of the files directory or one of the following constants for a subdirectory:

  • Environment.DIRECTORY_MUSIC
  • Environment.DIRECTORY_PODCASTS
  • Environment.DIRECTORY_RINGTONES
  • Environment.DIRECTORY_ALARMS
  • Environment.DIRECTORY_NOTIFICATIONS
  • Environment.DIRECTORY_PICTURES
  • Environment.DIRECTORY_MOVIES
getExternalFilesDirs(type) Returns absolute paths to application-specific directories on all shared/external storage devices where the application can place persistent files it owns.