获取设备内部存储不是应用程序目录和外部存储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 (将#修改为@)