Detailed explanation of internal storage and external storage (public directory, private directory, cache directory) in Android system

First of all, to clarify a concept, Android internal storage and external storage are not so-called mobile phone built-in memory is internal storage, SD card is external storage cloud.
Android does not physically distinguish between internal storage and external storage, but logically.

1. Concept

Internal storage refers to the internal storage space of the system. Logically distinguished by a directory is a folder under the
data directory. In addition to the data directory, there is an app directory to store apk information downloaded by the system, and a data directory to store various files.
For example:
The files stored by sharedPreferenced are in the / data / data // shared_prefs / directory.
The files stored by Sqlite are in the / data / data // database / directory.

External storage is much simpler
1. Mobile phone manufacturers bring their own storage, which is often said to be 16GB, 64GB, etc.
2. SD card
These are external storage

Second, obtain the operable internal storage directory (delete as the user deletes the app)

getCacheDir().getPath():内部缓存目录

/data/user/0/packname/cache/      //7.0以上
/data/data/packname/cache/



getFilesDir().getPath():内部文件目录

/data/user/0/packname/files/
/data/data/packname/files/

The shared_prefs and database folders have specialized tools such as sharedPreferenced and Sqlite to operate.

Note: The difference between data / data / ... and data / user / 0 / ... is that the latter distinguishes users, but this is not important

3. Obtain the operable external storage directory

External storage is divided into external public directory, external private directory, and cache directory:

1. Nine external public directories:

Environment.getExternalStoragePublicDirectory(context,type);

/storage/emulate/0/....

有九种type,分别对应不同的目录,如下

DIRECTORY_MUSIC:音乐类型    /storage/emulate/0/music
DIRECTORY_PICTURES:图片类型
DIRECTORY_MOVIES:电影类型
DIRECTORY_DCIM:照片类型,相机拍摄的照片视频都在这个目录(digital camera in memory) 
DIRECTORY_DOWNLOADS:下载文件类型   /storage/emulate/0/downloads
DIRECTORY_DOCUMENTS:文档类型
DIRECTORY_RINGTONES:铃声类型
DIRECTORY_ALARMS:闹钟提示音类型
DIRECTORY_NOTIFICATIONS:通知提示音类型

2. External private directory (deleted as the user deletes the app)

getExternalFilesDir(type)

/storage/emulated/0/Android/data/packagename/files/

3. External cache directory (deleted as the user deletes the app)

getExternalCacheDir()

/storage/emulated/0/Android/data/packname/cache/

4. External root directory

Environment.getExternalStorageDirectory()

/storage/emulated/0
path Method name Belong to
/data/data/<包名>/files getFilesDir() internal
/data/data/<包名>/cache getCacheDir() internal
/data/data/<包名>/app_ getDir () internal
/storage/emulated/0 Environment.getExternalStorageDirectory () External root
/storage/emulated/0/ Environment.getExternalStoragePublicDirectory(type) Nine external public directories
/storage/emulated/0/Android/data/<包名>/files/ getExternalFilesDir (type) External private directory
/storage/emulated/0/Android/data/<包名>/cache getExternalCacheDir () External cache directory

Note: No matter external or internal, as long as there is a package name in the path, it is private, and users need root to access. The methods for obtaining the path are all called by the Context, and are destroyed as the user deletes the app. The paths without the package name are called by the Environment.

Note:
Path analysis and corresponding directory of FileProvider:

The root-path corresponds to DEVICE_ROOT, which is File DEVICE_ROOT = new File ("/"), which is the root directory, and generally does not need to be configured.
files-path corresponds to the internal file directory obtained by getFilesDir ().
The cache-path corresponds to the internal cache directory obtained by getCacheDir ().
external-path corresponds to the external root directory pointed to by Environment.getExternalStorageDirectory ().
external-files-path corresponds to the external private directory obtained by ContextCompat.getExternalFilesDirs ().
external-cache-path corresponds to the external cache directory obtained by ContextCompat.getExternalCacheDirs ().

Published 60 original articles · 25 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_41466437/article/details/104970199
Recommended