Android Method for Obtaining Each Directory of the System

  • 2021-10-11 19:34:31
  • OfStack

During the development of Android, we often operate on the file system-storing and releasing the data we applied. Android system provides a variety of functional file directory, each directory has its own features and functions. This article mainly introduces and records each file directory commonly used in the actual development process under 1.

(1) Internal storage (Internal Storage)

Internal storage is the private directory of App. When an application is uninstalled, these files in internal storage are also deleted. The Shared Preferences and SQLite database files are stored on internal storage space.

-context.getFileDir()

Path: (data/data/Application Package Name/files),

However, after actual test (Huawei, Xiaomi mobile phone, etc.), the actual path of getFileDir is:/data/user/0/application package name/files


Log.e(TAG,getApplicationContext().getFilesDir().getAbsolutePath())

-context.getCacheDir()

The application's cache directory, where files are deleted first when the device runs out of memory, so there is no guarantee that files stored here may be thrown away at any time.

Path: (data/data/APPN/cache), but after actual test (Huawei, Xiaomi mobile phone, etc.), the mobile phone path of getCacheDir is:/data//data/user/0/APPN/cache


Log.e(TAG,getApplicationContext().getCacheDir().getAbsolutePath())

(2) External storage (External Storage)

External storage 1 generally refers to SDCard. Files in external storage can be modified by users or other applications. Directories in external storage are actually divided into two types:

-Public files (directories)

Files can be freely accessed, and the data of files are meaningful to other applications or users. When applications are uninstalled, the files created before uninstalling are still retained.


Environment.getExternalStorageDirectory() 

SD card root directory, path:/storage/emulated/0


Log.e(TAG, Environment.getExternalStorageDirectory().getAbsolutePath());

-Private files

Because of external storage, even this type of file can be accessed by other programs, but an application's private file has no access value to other applications (except malicious programs). On external storage, the value of applying private files is that after unloading, these files will also be deleted. Similar to internal storage.


getApplicationContext().getExternalFilesDir( " ) 

Path:/storage/emulated/0/Android/data/Application Package Name/files


Log.e(TAG, getApplicationContext().getExternalFilesDir("").getAbsolutePath());

Extension: getExternalFilesDir is the operation object of setting → application → specific application details → data clearing in mobile phone


getApplicationContext().getExternalCacheDir() 

Path:/storage/emulated/0/Android/data/Application Package Name/cache


Log.e(TAG,getApplicationContext().getExternalCacheDir().getAbsolutePath());

Extension: getExternalCacheDir is the operation object of setting → application → specific application details → clearing cache in mobile phone


Related articles: