String filesDir_toString = getFilesDir().toString();
String cacheDir_toString = getCacheDir().toString()
String external_cacheDir_toString = getExternalCacheDir().toString()
String custom = getExternalFilesDir("custom").toString();
下面是测试代码
public class Main2Activityextends AppCompatActivity {
private static StringTAG ="Shark";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
/**
* 获取当前程序路径 应用在内存上的目录 :/data/data/{packageName}/files
* 获取内置存储下的文件目录,可以用来保存不能公开给其他应用的一些敏感数据如用户个人信息。
*
* /data/data/应用包名/files/[6.0以下]
* /data/user/0/应用包名/files/[6.0以上]=[Cocos2dx下的getWritablePath()]
*/
// /data/user/0/{packageName}/files
String filesDir_toString = getFilesDir().toString();
// /data/user/0/{packageName}/files
String filesDir_getAbsolutePath = getFilesDir().getAbsolutePath();
// /data/user/0/{packageName}
String filesDir_getParent = getFilesDir().getParent();
// /data/user/0/{packageName}/files
String filesDir_getPath = getFilesDir().getPath();
try {
// /data/data/{packageName}/files
String filesDir_getCanonicalPath = getFilesDir().getCanonicalPath();
Log.i(TAG, " =======================================================================");
Log.i(TAG, " filesDir_getCanonicalPath===" + filesDir_getCanonicalPath);
}catch (IOException e) {
e.printStackTrace();
}
// files
String filesDir_getName = getFilesDir().getName();
// true
boolean filesDir_canExecute = getFilesDir().canExecute();
// true
boolean filesDir_canRead = getFilesDir().canRead();
// true
boolean filesDir_canWrite = getFilesDir().canWrite();
// -----------------------------------------------------------------------------------------
Log.i(TAG, " filesDir_toString ===" + filesDir_toString);
Log.i(TAG, " filesDir_getAbsolutePath===" + filesDir_getAbsolutePath);
Log.i(TAG, " filesDir_getName===" + filesDir_getName);
Log.i(TAG, " filesDir_getParent===" + filesDir_getParent);
Log.i(TAG, " filesDir_getPath ===" + filesDir_getPath);
Log.i(TAG, " filesDir_canExecute===" + filesDir_canExecute);
Log.i(TAG, " filesDir_canRead===" + filesDir_canRead);
Log.i(TAG, " filesDir_canWrite===" + filesDir_canWrite);
Log.i(TAG, " =======================================================================\n\n\n");
// =========================================================================================
/**
* 应用的在内存上的缓存目录 :/data/data/{packageName}/cache
* 获取内置存储下的缓存目录,可以用来保存一些缓存文件如图片,当内置存储的空间不足时将系统自动被清除。
*
* /data/data/应用包名/cache/[6.0以下]
* /data/user/0/应用包名/cache/[6.0以上]
*/
// /data/user/0/{packageName}/cache
String cacheDir_toString = getCacheDir().toString();
// /data/user/0/{packageName}/cache
String cacheDir_getAbsolutePath = getCacheDir().getAbsolutePath();
// /data/user/0/{packageName}
String cacheDir_getParent = getCacheDir().getParent();
// /data/user/0/{packageName}/cache
String cacheDir_getPath = getCacheDir().getPath();
try {
// /data/data/{packageName}/cache
String cacheDir_getCanonicalPath = getCacheDir().getCanonicalPath();
Log.i(TAG, " =======================================================================");
Log.i(TAG, " cacheDir_getCanonicalPath===" + cacheDir_getCanonicalPath);
}catch (IOException e) {
e.printStackTrace();
}
// cache
String cacheDir_getName = getCacheDir().getName();
// true
boolean cacheDir_canExecute = getCacheDir().canExecute();
// true
boolean cacheDir_canRead = getCacheDir().canRead();
// true
boolean cacheDir_canWrite = getCacheDir().canWrite();
// -----------------------------------------------------------------------------------------
Log.i(TAG, " cacheDir_toString ===" + cacheDir_toString);
Log.i(TAG, " cacheDir_getAbsolutePath===" + cacheDir_getAbsolutePath);
Log.i(TAG, " cacheDir_getName===" + cacheDir_getName);
Log.i(TAG, " cacheDir_getParent===" + cacheDir_getParent);
Log.i(TAG, " cacheDir_getPath ===" + cacheDir_getPath);
Log.i(TAG, " cacheDir_canExecute===" + cacheDir_canExecute);
Log.i(TAG, " cacheDir_canRead===" + cacheDir_canRead);
Log.i(TAG, " cacheDir_canWrite===" + cacheDir_canWrite);
Log.i(TAG, " =======================================================================");
// =========================================================================================
/**
* 应用在外部存储上的目录 :/storage/emulated/0/Android/data/{packageName}/files/Movies
*
* 这个需要传入 一个String type
*
* 同上面一样的,这里的文件会随着App卸载而被删除,也可以由用户手动在设置界面里面清除。
* 既然是SD卡上的目录,那么是可以被其他的应用读取到的,所以这个目录下,不应该存放用户的敏感信息。
*/
try {
// /storage/emulated/0/Android/data/{packageName}/files/Music
String music = getExternalFilesDir(Environment.DIRECTORY_MUSIC).toString();
String pictures = getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString();
String movies = getExternalFilesDir(Environment.DIRECTORY_MOVIES).toString();
String download = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
String dcim = getExternalFilesDir(Environment.DIRECTORY_DCIM).toString();
// 自定义
String custom = getExternalFilesDir("custom").toString();
Log.i(TAG, "=======================================================================");
Log.i(TAG, "music ===" + music);
Log.i(TAG, "pictures ===" + pictures);
Log.i(TAG, "movies ===" + movies);
Log.i(TAG, "download ===" + download);
Log.i(TAG, "dcim ===" + dcim);
Log.i(TAG, "custom ===" + custom);
Log.i(TAG, "=======================================================================");
}catch (Exception e) {
}
// =========================================================================================
/**
* 应用的在内存上的缓存目录 :/data/data/{packageName}/cache
* 获取内置存储下的缓存目录,可以用来保存一些缓存文件如图片,当内置存储的空间不足时将系统自动被清除。
* 同上面一样的,这里的文件会随着App卸载而被删除,也可以由用户手动在设置界面里面清除。
* 既然是SD卡上的目录,那么是可以被其他的应用读取到的,所以这个目录下,不应该存放用户的敏感信息。
*/
// /storage/emulated/0/Android/data/{packageName}/cache
String external_cacheDir_toString = getExternalCacheDir().toString();
String external_cacheDir_getAbsolutePath = getExternalCacheDir().getAbsolutePath();
String external_cacheDir_getParent = getExternalCacheDir().getParent();
String external_cacheDir_getPath = getExternalCacheDir().getPath();
try {
String external_cacheDir_getCanonicalPath = getExternalCacheDir().getCanonicalPath();
Log.i(TAG, " =======================================================================");
Log.i(TAG, " external_cacheDir_getCanonicalPath===" + external_cacheDir_getCanonicalPath);
}catch (IOException e) {
e.printStackTrace();
}
// cache
String external_cacheDir_getName = getExternalCacheDir().getName();
// true
boolean external_cacheDir_canExecute = getExternalCacheDir().canExecute();
// true
boolean external_cacheDir_canRead = getExternalCacheDir().canRead();
// true
boolean external_cacheDir_canWrite = getExternalCacheDir().canWrite();
// -----------------------------------------------------------------------------------------
Log.i(TAG, " external_cacheDir_toString ===" + external_cacheDir_toString);
Log.i(TAG, " external_cacheDir_getAbsolutePath===" + external_cacheDir_getAbsolutePath);
Log.i(TAG, " external_cacheDir_getName===" + external_cacheDir_getName);
Log.i(TAG, " external_cacheDir_getParent===" + external_cacheDir_getParent);
Log.i(TAG, " external_cacheDir_getPath ===" + external_cacheDir_getPath);
Log.i(TAG, " external_cacheDir_canExecute===" + external_cacheDir_canExecute);
Log.i(TAG, " external_cacheDir_canRead===" + external_cacheDir_canRead);
Log.i(TAG, " external_cacheDir_canWrite===" + external_cacheDir_canWrite);
Log.i(TAG, " =======================================================================");
// String filePath = getExternalFilesDir("custom") + File.separator + "fileUtilDir" + File.separator + "file.txt";
// Log.w(TAG,"filePath="+filePath);
// String content = "将字符串内容添加到文本文件中";
// boolean writeFileState = FileUtils.writeFile(filePath,content);
// Log.w(TAG,"writeFileState="+writeFileState);
// =========================================================================================
// /data 返回用户数据目录。 内置存储 data 分区 根目录
String dataDirectory = Environment.getDataDirectory().toString();
// /data/cache 返回下载/缓存内容目录。
String getDownloadCacheDirectory = Environment.getDownloadCacheDirectory().toString();
// /system 返回包含核心Android操作系统的“系统”分区的根目录。始终以只读形式存在和挂载。
// 系统分区
String getRootDirectory = Environment.getRootDirectory().toString();
// /storage/emulated/0 返回主共享/外部存储目录。 获取外置内存跟目录
String getExternalStorageDirectory = Environment.getExternalStorageDirectory().toString();
// mounted 返回主共享/外部存储媒体的当前状态。
String getExternalStorageState = Environment.getExternalStorageState();
}
}
相关资料 1.https://blog.csdn.net/csdn_aiyang/article/details/80665185
2.https://blog.csdn.net/nimeghbia/article/details/64121432
3.https://blog.csdn.net/ArisKing/article/details/88263183
4.https://blog.csdn.net/itermeng/article/details/79423035