一、存储类型与权限说明
内部存储(Internal Storage)
路径:/data/data/包名/files/(应用私有,其他应用无法访问,卸载应用时自动删除)
用途:保存敏感数据(如用户配置、小文件)
权限:无需申请任何权限
外部存储(External Storage)
私有目录:/storage/emulated/0/Android/data/包名/files/(应用私有,卸载时删除)
公共目录:/storage/emulated/0/Pictures/、/DCIM/、/Download/ 等(所有应用可访问,需权限)
权限:
Android 6.0(API 23)以上需动态申请 WRITE_EXTERNAL_STORAGE/READ_EXTERNAL_STORAGE
Android 10 以上推荐使用分区存储,无需申请传统存储权限
二、内部存储
内部存储使用简单,不需要申请权限
//这里也可以不使用Environment.DIRECTORY_DOWNLOADS,自定义文件夹名称
getDir(Environment.DIRECTORY_DOWNLOADS, 0).let { downloadDir ->
if (downloadDir != null && downloadDir.exists()) {
Timber.tag(TAG).i("Download directory exists: %s", downloadDir.absolutePath)
} else {
Timber.tag(TAG).w("Download directory does not exist or is null")
}
}
filesDir.let { filesDir ->
Timber.tag(TAG).i("Files directory: %s", filesDir.absolutePath)
}
cacheDir.let { cacheDir ->
Timber.tag(TAG).i("Cache directory: %s", cacheDir.absolutePath)
}
codeCacheDir.let { codeCacheDir ->
Timber.tag(TAG).i("Code cache directory: %s", codeCacheDir.absolutePath)
}
fileList().iterator()
.forEach { file ->
Timber.tag(TAG).i("File in internal storage: %s", file)
}
以上控制台输出:
ChatH...vity Download directory exists: /data/user/0/com.xxx/app_app_ddd
ChatH...vity Files directory: /data/user/0/com.xxx/files
ChatH...vity Cache directory: /data/user/0/com.xxx/cache
ChatH...vity Code cache directory: /data/user/0/com.xxx/code_cache
fileList的文件名,也就是/data/user/0/com.xxx/files下的文件名
ContextWrapper有一些文件的操作类,比如文件的读写操作:
@Override
public FileInputStream openFileInput(String name)
throws FileNotFoundException {
return mBase.openFileInput(name);
}
@Override
public FileOutputStream openFileOutput(String name, int mode)
throws FileNotFoundException {
return mBase.openFileOutput(name, mode);
}
三、外部存储
参考:https://blog.youkuaiyun.com/dsc114/article/details/149941070
Android文件存储类型与权限说明
2903

被折叠的 条评论
为什么被折叠?



