一、Android存储简介
Android系统分为内部存储和外部存储
从Android6.0开始不断在更新存储权限
外部存储路径的开头:storage/emulated/0
内部存储文件路径的开头:/data/user/0/应用的包名(packageName)
在设备上对应的目录为/data/date/应用的包名(packageName)
系统的公共存储文件夹
二、存储文件路径
TestActivity.kt代码
package example.demo.kotlin
import android.os.Bundle
import android.os.Environment
import androidx.appcompat.app.AppCompatActivity
import example.demo.kotlin.utils.LogUtil
/**
***********************************************************************
*Author: zbl
*Time: 2020/1/19 9:24
*Name:
*Overview:
*Usage:
***********************************************************************
*/
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.filesDir
val cacheDir = this.cacheDir
val externalCacheDir = this.externalCacheDir
val 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/files
activity 获取缓存文件路径 cacheDir : /data/user/0/example.demo.kotlin/cache
activity 获取缓存文件路径 externalCacheDir : /storage/emulated/0/Android/data/example.demo.kotlin/cache
activity 获取缓存文件路径 externalCacheFilesDirPictures : /storage/emulated/0/Android/data/example.demo.kotlin/files/Pictures
*/
val mFilesDir2 = this.application.filesDir
val cacheDir2 = this.application.cacheDir
val externalCacheDir2 = this.application.externalCacheDir
val 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/files
activity.getApplication() 获取缓存文件路径 cacheDir2: /data/user/0/example.demo.kotlin/cache
activity.getApplication() 获取缓存文件路径 externalCacheDir2 : /storage/emulated/0/Android/data/example.demo.kotlin/cache
activity.getApplication() 获取缓存文件路径 externalCacheFilesDirPictures2: /storage/emulated/0/Android/data/example.demo.kotlin/files/Pictures
*/
val filesDir3 = this.applicationContext.filesDir
val cacheDir3 = this.applicationContext.cacheDir
val externalCacheDir3 = this.applicationContext.externalCacheDir
val 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/files
activity.getApplicationContext() 获取缓存文件路径 cacheDir3 : /data/user/0/example.demo.kotlin/cache
activity.getApplicationContext() 获取缓存文件路径 externalCacheDir3 : /storage/emulated/0/Android/data/example.demo.kotlin/cache
activity.getApplicationContext() 获取缓存文件路径 externalCacheFilesDirPictures3 : /storage/emulated/0/Android/data/example.demo.kotlin/files/Pictures
*/
}
}