android 各个目录获取
不会创建文件,但是会创建databases目录
getApplicationContext().getDatabasePath("dataBase1.db") 查询的时候要带着.db,也就是包含数据库名字的后缀
会创建目录的的接口(一般的getDir接口都会创建目录)
getApplicationContext().getExternalFilesDir 只要调用到这个借口,就会创建/storage/emulated/0/Android/data/xxx/files文件夹
getApplicationContext().getDir("222", Context.MODE_PRIVATE)
getApplicationContext().getDataDir()(/data/user/0/com.android.test应用启动就有了)
getApplicationContext().getFilesDir()
getApplicationContext().getCacheDir()
getApplicationContext().getCodeCacheDir()
getApplicationContext().getExternalFilesDir("111")
getApplicationContext().getExternalFilesDirs("111")
private void fun() {
/*
* ---111---: getDataDir:cache
* ---111---: getDataDir:code_cache
* ---111---: getDataDir:files
* ---111---: getDataDir:shared_prefs
* ---111---: getDataDir:databases
* ---111---: getDataDir:app_shared_prefs
* ---111---: getDataDir:no_backup
*/
Arrays.stream(Objects.requireNonNull(getApplicationContext().getDataDir().list())).forEach(dirName -> {
Log.i("---111---", "getDataDir:" + dirName);
});
/*
* ---111---: getDataDir.listFiles:/data/user/0/com.android.test/cache
* ---111---: getDataDir.listFiles:/data/user/0/com.android.test/code_cache
* ---111---: getDataDir.listFiles:/data/user/0/com.android.test/files
* ---111---: getDataDir.listFiles:/data/user/0/com.android.test/shared_prefs
* ---111---: getDataDir.listFiles:/data/user/0/com.android.test/databases
* ---111---: getDataDir.listFiles:/data/user/0/com.android.test/app_shared_prefs
* ---111---: getDataDir.listFiles:/data/user/0/com.android.test/no_backup
*/
Arrays.stream(Objects.requireNonNull(getApplicationContext().getDataDir().listFiles())).forEach(file -> {
Log.i("---111---", "getDataDir.listFiles:" + file.getAbsolutePath());
});
/**
* ---111---: getDataDir:/data/user/0/com.android.test
* ---111---: getFilesDir:/data/user/0/com.android.test/files
* ---111---: getCacheDir:/data/user/0/com.android.test/cache
* ---111---: getCodeCacheDir:/data/user/0/com.android.test/code_cache
* ---111---: getDatabasePath:/data/user/0/com.android.test/databases/dataBase1.db
* ---111---: getDir:/data/user/0/com.android.test/app_111
*/
Log.i("---111---", "getDataDir:" + getApplicationContext().getDataDir().getAbsolutePath());
Log.i("---111---", "getFilesDir:" + getApplicationContext().getFilesDir().getAbsolutePath());
Log.i("---111---", "getCacheDir:" + getApplicationContext().getCacheDir().getAbsolutePath());
Log.i("---111---", "getCodeCacheDir:" + getApplicationContext().getCodeCacheDir().getAbsolutePath());
Log.i("---111---", "getDatabasePath:" + getApplicationContext().getDatabasePath("dataBase1.db").getAbsolutePath());
Log.i("---111---", "getDir***:" + getApplicationContext().getDir("111", Context.MODE_PRIVATE).getAbsolutePath());
/**
* ---111---:
* getPackageCodePath:/data/app/~~qL08Zu5rx2uuRQWwygZzaQ==/com.android.test-gJJEOgibwXHnKgzLSPlCjQ==/base.apk
* ---111---:
* getPackageResourcePath:/data/app/~~qL08Zu5rx2uuRQWwygZzaQ==/com.android.test-gJJEOgibwXHnKgzLSPlCjQ==/base.apk
*/
Log.i("---111---", "getPackageCodePath:" + getApplicationContext().getPackageCodePath());
Log.i("---111---", "getPackageResourcePath:" + getApplicationContext().getPackageResourcePath());
/**
* ---111---: getExternalFilesDir(materials):/storage/emulated/0/Android/data/com.android.test/files/materials
* ---111---: getExternalFilesDir(null):/storage/emulated/0/Android/data/com.android.test/files
* ---111---: getExternalFilesDirs:/storage/emulated/0/Android/data/com.android.test/files
* ---111---: getExternalCacheDir:/storage/emulated/0/Android/data/com.android.test/cache
* ---111---: getExternalCacheDirs:/storage/emulated/0/Android/data/com.android.test/cache
*/
Log.i("---111---", "getExternalFilesDir(materials):" + getApplicationContext().getExternalFilesDir("materials").getAbsolutePath());
Log.i("---111---", "getExternalFilesDir(null):" + getApplicationContext().getExternalFilesDir(null).getAbsolutePath());
Arrays.stream(Objects.requireNonNull(getApplicationContext().getExternalFilesDirs(null))).forEach(file -> {
Log.i("---111---", "getExternalFilesDirs:" + file.getAbsolutePath());
});
Log.i("---111---", "getExternalCacheDir:" + getApplicationContext().getExternalCacheDir().getAbsolutePath());
Arrays.stream(Objects.requireNonNull(getApplicationContext().getExternalCacheDirs())).forEach(file -> {
Log.i("---111---", "getExternalCacheDirs:" + file.getAbsolutePath());
});
/**
* ---111---: getNoBackupFilesDir:/data/user/0/com.android.test/no_backup
*/
Log.i("---111---", "getNoBackupFilesDir:" + getApplicationContext().getNoBackupFilesDir().getAbsolutePath());
}
关于外部存储getExternalFilesDir和getExternalFilesDirs区别
首先,简单介绍下两个方法的差异以及主外部存储。
先看下这段代码,
String state = Environment.getExternalStorageState();
File externalFile = context.getExternalFilesDir( null );
File[] externalFiles = context.getExternalFilesDirs( Environment.DIRECTORY_PICTURES );
for (File file : externalFiles) {
Log.d( TAG, "state="+ state + ";\nexternalFiles=" + file + ";\nexternalFile="+externalFile);
try {
FileOutputStream fileOutputStream = new FileOutputStream( new File( file, "aaaa.txt" ) );
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
运行的手机支持SD卡 并插入了一张SD卡,看看运行结果
2019-07-15 14:46:07.819 12704-12704/com.flx.testfilestorage D/flx_storage: state=mounted;
externalFiles=/storage/emulated/0/Android/data/com.flx.testfilestorage/files/Pictures;
externalFile=/storage/emulated/0/Android/data/com.flx.testfilestorage/files
2019-07-15 14:46:07.821 12704-12704/com.flx.testfilestorage D/flx_storage: state=mounted;
externalFiles=/storage/553C-0E05/Android/data/com.flx.testfilestorage/files/Pictures;
externalFile=/storage/emulated/0/Android/data/com.flx.testfilestorage/files
从上面看到,getExternalFilesDirs获取的有两个外部存储,getExternalFilesDir是一个。这两个外部存储,一个是主外部存储 即手机本身存储中分为 内部存储和外部存储的 外部存储部分,另一个是SD卡的挂载路径。
getExternalFilesDir(),获取就是主外部存储路径。
getExternalFilesDirs(),获取所有外部存储的路径,包括本身的外部存储 和 扩展出来的存储(如SD卡)。
在一开始就说过,应用存储到外部存储的文件 当应用卸载时只有getExternalFilesDir()路径下的会被删除。
上面代码在log后,所有外部存储路径下 都创建了aaaa.txt的文件,实际操作结果也是符合的,当卸载应用时,/storage/553C-0E05/Android/data/com.flx.testfilestorage/files/这个下面的aaaa.txt 仍然存在的。
Environment获取外部存储
Log.i(TAG, "getExternalStorageDirectory:" + Environment.getExternalStorageDirectory().getAbsolutePath());
Log.i(TAG, "getDataDirectory:" + Environment.getDataDirectory().getAbsolutePath());
Log.i(TAG, "getStorageDirectory:" + Environment.getStorageDirectory());
Log.i(TAG, "getRootDirectory:" + Environment.getRootDirectory());
Log.i(TAG, "getDownloadCacheDirectory:" + Environment.getDownloadCacheDirectory());
Log.i(TAG, "getExternalStoragePublicDirectory(Download):" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
Log.i(TAG, "getExternalStoragePublicDirectory(shared):" + Environment.getExternalStoragePublicDirectory(Environment.MEDIA_SHARED).getAbsolutePath());
// I/MainActivity---test---: getExternalStorageDirectory:/storage/emulated/0
// I/MainActivity---test---: getDataDirectory:/data
// I/MainActivity---test---: getStorageDirectory:/storage
// I/MainActivity---test---: getRootDirectory:/system
// I/MainActivity---test---: getDownloadCacheDirectory:/data/cache
// I/MainActivity---test---: getExternalStoragePublicDirectory(111):/storage/emulated/0/111
// I/MainActivity---test---: getExternalStoragePublicDirectory(Download):/storage/emulated/0/Download
// I/MainActivity---test---: getExternalStoragePublicDirectory(shared):/storage/emulated/0/shared