Android Save File Path Method
- 2021-10-11 19:29:52
- OfStack
Android Save File to Local Path Problem
Common path
For example: application
Package name: com. my. company
Project name: chat
/data/data == ///data/user/0
getExternalFilesDir() Method can get the SDCard/Android/data/ Package name of your application /files/ Directory,
1 General release 1 Some data stored for a long time
getExternalCacheDir() Method can get the SDCard/Android/data/ Your application package name /cache/ Directory,
1 Store temporary cache data in general
If you use the above method, when your application is uninstalled by the user, SDCard/Android/data/ Package name of your application /
All files in this directory will be deleted without leaving spam.
And up there 2 The directories correspond to each other Settings -> Application -> Apply the "Clear Data" and "Clear Cache" options in the details
getCacheDir() Method is used to get the /data/data/<application package>/cache Directory
getFilesDir() Method is used to get the /data/data/<application package>/files Directory
Save 1 picture hearder. png to Files
String path = mContext.getFilesDir().getAbsolutePath();
path = path + File.separator + directoryName + File.separator + fileName;
directoryName : chat
fileName : hearder.png
Get path That is : ///data/user/0/com.my.company/files/chat/hearder.png
Save the picture to dir
String path = mContext.getDir(directoryName, Context.MODE_PRIVATE).getAbsolutePath();
path = path + File.separator + fileName;
directoryName : chat Note: directory can not has "/"
fileName : hearder.png
Get path That is : ///data/user/0/com.my.company/app_chat/hearder.png
Get the path code
public String getDiskCacheDir(Context context) {
String cachePath = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return cachePath;
}
As you can see, when the SD card exists or the SD card cannot be removed, the getExternalCacheDir () method is called to get the cache path, otherwise the getCacheDir () method is called to get the cache path. The former gets the/sdcard/Android/data/com. your. company/cache, while the latter gets the/data/data/com. your. company/cache.
Path classification
When the android program scans the storage, if the default path is obtained by using API: Environment. getExternalStorageDirectory (). getPath (), it can be judged that Environment. getExternalStorageDirectory (). getParentFile () first. If null is returned, there is no parent path, and Environment. getExternalStorageDirectory (). getPath () is the current parent path.
Environment.getDataDirectory() = /data
Environment.getDownloadCacheDirectory() = /cache
Environment.getExternalStorageDirectory() = /mnt/sdcard
Environment.getExternalStoragePublicDirectory( " test " ) = /mnt/sdcard/test
Environment.getRootDirectory() = /system
getPackageCodePath() = /data/app/com.my.app-1.apk
getPackageResourcePath() = /data/app/com.my.app-1.apk
getCacheDir() = /data/data/com.my.app/cache
getDatabasePath( " test " ) = /data/data/com.my.app/databases/test
getDir( " test " , Context.MODE_PRIVATE)= /data/data/com.my.app/app_test
getExternalCacheDir() = /mnt/sdcard/Android/data/com.my.app/cache
getExternalFilesDir( " test " )= /mnt/sdcard/Android/data/com.my.app/files/test
getExternalFilesDir(null) = /mnt/sdcard/Android/data/com.my.app/files
getFilesDir() = /data/data/com.my.app/files