Android文件路径的总结:
学习内容:
创建文件(设置文件路径的方法) 1.context.getFilesDir(),对应的路径:/data/user/0/工程包名/files 2.context.getCacheDir(),对应的路径:/data/user/0/工程包名/cache 3.context.getExternalCacheDir(),路径:/storage/emulated/0/Android/data/包名/cache 4.context.getExternalFilesDir(String),路径:/storage/emulated/0/Android/data/包名/files 5.Environment.getExternalStorageDirectory(),路径:/storage/emulated/0 6.Environment.DIRECTORY_DOWNLOADS,路径:/storage/emulated/0/Download
01.注意,具体的路径查看: sdcard(相当于:/storage/emulated/0) -> Android/data/工程包名/files/
02.注意,创建文件的方法:
private fun createFile(): File {
val root = getExternalFilesDir(null)?.absolutePath
val file = File(root, "updateDemo.apk")
if (file.exists()) {
file.delete()
}
try {
file.createNewFile()
return file
} catch (e: IOException) {
e.printStackTrace()
}
Log.i("hy55", "createFile file=$file")
return file
}
03.安装apk需要的权限:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- <uses-permission android:name="android.permission.INSTALL_PACKAGES"-->
<!-- tools:ignore="ProtectedPermissions" />-->
<uses-permission android:name='android.permission.INTERNET'/> <!-- 联网权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 写入SD卡权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" /> <!-- 在SD卡中创建和删除文件的权限 -->
|