一、Android存储简介
Android系统分为内部存储和外部存储
从Android6.0开始不断在更新存储(读写)权限,除了在AndroidManifest.xml文件里声明,app运行时也要动态申请使用对应的权限
提醒:应用私有存储不需要动态申请权限
- Context.getFileDir();获取内置存储下的文件目录,可以用来保存不能公开给其他应用的一些敏感数据如用户个人信息。
路径为:/data/data/应用包名/files/- Context.getCacheDir();获取内置存储下的缓存目录,可以用来保存一些缓存文件如图片,当内置存储的空间不足时将系统自动被清除。
路径为: /data/data/应用包名/cache/
外部存储路径的开头:storage/emulated/0
内部存储文件路径的开头:/data/user/0/应用的包名(packageName)
在设备上对应的目录为/data/date/应用的包名(packageName)
系统的公共存储文件夹
二、存储文件路径
TestActivity.kt代码
package example.demo.kotlinimport android.os.Bundle
import android.os.Environment
import androidx.appcompat.app.AppCompatActivity
import example.demo.kotlin.utils.LogUtil/**************************************************************************Author: chinadragon*Time: 2020/1/19 9:24************************************************************************/
class TestActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.actvity_test)//getExternalStorageDirectory在安卓10中已弃用, 可以使用getExternalFilesDir 代替val externalStorageDirectory = Environment.getExternalStorageDirectory()val filesDir = this.filesDirval cacheDir = this.cacheDirval externalCacheDir = this.externalCacheDirval externalCacheFilesDirPictures = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES)LogUtil.i("activity 获取缓存文件路径 filesDir : ${filesDir.absolutePath}")LogUtil.i("activity 获取缓存文件路径 cacheDir : ${cacheDir.absolutePath}")externalCacheDir?.let {LogUtil.i("activity 获取缓存文件路径 externalCacheDir : ${it.absolutePath}")}externalCacheFilesDirPictures?.let {LogUtil.i("activity 获取缓存文件路径 externalCacheFilesDirPictures : ${it.absolutePath}")}/*activity 获取缓存文件路径 filesDir : /data/user/0/example.demo.kotlin/filesactivity 获取缓存文件路径 cacheDir : /data/user/0/example.demo.kotlin/cacheactivity 获取缓存文件路径 externalCacheDir : /storage/emulated/0/Android/data/example.demo.kotlin/cacheactivity 获取缓存文件路径 externalCacheFilesDirPictures : /storage/emulated/0/Android/data/example.demo.kotlin/files/Pictures*/val mFilesDir2 = this.application.filesDirval cacheDir2 = this.application.cacheDirval externalCacheDir2 = this.application.externalCacheDirval externalCacheFilesDirPictures2 = this.application.getExternalFilesDir(Environment.DIRECTORY_PICTURES)LogUtil.i("activity.getApplication() 获取缓存文件路径 filesDir2 : ${mFilesDir2.absolutePath}")LogUtil.i("activity.getApplication() 获取缓存文件路径 cacheDir2: ${cacheDir2.absolutePath}")externalCacheDir2?.let {LogUtil.i("activity.getApplication() 获取缓存文件路径 externalCacheDir2 : ${it.absolutePath}")}externalCacheFilesDirPictures2?.let {LogUtil.i("activity.getApplication() 获取缓存文件路径 externalCacheFilesDirPictures2: ${it.absolutePath}")}/*activity.getApplication() 获取缓存文件路径 filesDir2 : /data/user/0/example.demo.kotlin/filesactivity.getApplication() 获取缓存文件路径 cacheDir2: /data/user/0/example.demo.kotlin/cacheactivity.getApplication() 获取缓存文件路径 externalCacheDir2 : /storage/emulated/0/Android/data/example.demo.kotlin/cacheactivity.getApplication() 获取缓存文件路径 externalCacheFilesDirPictures2: /storage/emulated/0/Android/data/example.demo.kotlin/files/Pictures*/val filesDir3 = this.applicationContext.filesDirval cacheDir3 = this.applicationContext.cacheDirval externalCacheDir3 = this.applicationContext.externalCacheDirval externalCacheFilesDirPictures3 = this.applicationContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES)LogUtil.i("activity.getApplicationContext() 获取缓存文件路径 filesDir3 : ${filesDir3.absolutePath}")LogUtil.i("activity.getApplicationContext() 获取缓存文件路径 cacheDir3 : ${cacheDir3.absolutePath}")externalCacheDir3?.let {LogUtil.i("activity.getApplicationContext() 获取缓存文件路径 externalCacheDir3 : ${it.absolutePath}")}externalCacheFilesDirPictures3?.let {LogUtil.i("activity.getApplicationContext() 获取缓存文件路径 externalCacheFilesDirPictures3 : ${it.absolutePath}")}/*activity.getApplicationContext() 获取缓存文件路径 filesDir3 : /data/user/0/example.demo.kotlin/filesactivity.getApplicationContext() 获取缓存文件路径 cacheDir3 : /data/user/0/example.demo.kotlin/cacheactivity.getApplicationContext() 获取缓存文件路径 externalCacheDir3 : /storage/emulated/0/Android/data/example.demo.kotlin/cacheactivity.getApplicationContext() 获取缓存文件路径 externalCacheFilesDirPictures3 : /storage/emulated/0/Android/data/example.demo.kotlin/files/Pictures*/}
}