如何解决获取设备内部存储不是应用程序目录和外部存储SD CARD,这是正确的方法吗?
我浏览了大量文章(我的意思是太多,无法在此列出)以找出找到设备存储和 SD 卡根文件夹的最佳方法。 找到sd卡路径的那个是来自this的gist,找到根内部存储路径还是很困难的。
我现在不想使用存储访问框架,我的目标是带有 android:requestLegacyExternalStorage="true"
标志的 android 10。
最后,我找到了一种让它工作的方法,它看起来很容易,几乎好得令人难以置信。所以我想验证一下是否可以继续执行此代码,而不会在将来陷入不可预见的陷阱。
这是我的设备的根文件夹(三星 A50s - SM-A507fN/DS 128GB) 内置和外置 SD 卡
private fun getStoragePaths() {
val mediaStorageDir = applicationContext.getExternalFilesDirs(null)
mediaStorageDir.forEach {
val path = it.absolutePath.toString()
log("complete path $path")
val rootPath = path.substring(0,path.indexOf("Android"))
log("root path $rootPath")
}
}
我的日志
2021-04-21 11:59:00.501 4037-4037/com.pt.workingwithstorage I/myStorage: complete path /storage/emulated/0/Android/data/com.pt.workingwithstorage/files
2021-04-21 11:59:00.501 4037-4037/com.pt.workingwithstorage I/myStorage: root path /storage/emulated/0/
2021-04-21 11:59:00.501 4037-4037/com.pt.workingwithstorage I/myStorage: complete path /storage/0C5B-9496/Android/data/com.pt.workingwithstorage/files
2021-04-21 11:59:00.501 4037-4037/com.pt.workingwithstorage I/myStorage: root path /storage/0C5B-9496/
当我使用完整路径 /storage/emulated....../files
既没有创建文件也没有创建文件夹。
但是使用我子串的根路径工作得很好。
以后我可以继续这样做吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。