【问题标题】:Is it possible to get Android SD card location on SAMSUNG Tablet / Android 11是否可以在 SAMSUNG 平板电脑/Android 11 上获取 Android SD 卡位置
【发布时间】:2022-08-14 04:16:32
【问题描述】:

我查看了许多关于获取 SD 卡位置的帖子,其中大多数提到使用 Environment.getExternalStorageState() Environment.getExternalStorageDirectory() 总是为我在三星 Galaxy Tab A / Android 11 (/storage/emulated/0) 上返回内部存储

我确实看到了一条评论说使用System.getenv(\"SECONDARY_STORAGE\"),它返回/storage/sdcard:/storage/usb1:/storage/usb2,但试图打开一个文件(我已经放在那里)只会返回一个not found错误(即java.io.FileNotFoundException: /sdcard/external_sd/Music/test/testfile.mp3: open failed: ENOENT (No such file or directory)

也试过Environment.getExternalStoragePublicDirectory(Environment.MEDIA_MOUNTED).getPath(),这返回/storage/emulated/0/mounted,这也不起作用

我有以下权限..

<uses-permission android:name=\"android.permission.READ_EXTERNAL_STORAGE\" />
<uses-permission android:name=\"android.permission.PERMISSIONS_STORAGE\" />
<uses-permission android:name=\"android.permission.REQUEST_EXTERNAL_STORAGE\" />

在启动时我打电话

 private fun verifyStoragePermissions(activity: Activity) {
    // Check if we have write permission
    val permission =
        ActivityCompat.checkSelfPermission(activity, READ_EXTERNAL_STORAGE)

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don\'t have permission so prompt the user
        ActivityCompat.requestPermissions(
            activity,
            arrayOf(READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE),
            
        )
    }
}

最后,如here 所述,我硬连线/sdcard/external_sd 但这也不起作用(即我尝试打开一个我知道在那里的文件,并使用以下内容列出内容..

File(testFolder).walk().forEach {
            println(it)
        }

我有其他应用程序写入它,所以必须有某种方法(我只想读取现有文件)。

我可能做错了什么?

  • \"I have the following permissions\" -- 后两个不是实际权限。

标签: android kotlin


【解决方案1】:

你可以试试这个:

File[] dirs = getExternalFilesDirs("");
for (File currD :
        dirs) {
    if (currD.getAbsolutePath().equals(getExternalFilesDir("").getAbsolutePath())) {
        // Here is internal storage path
    } else {
        // Here is SD path
    }
}

【讨论】:

  • 这已经足够接近了。第一次尝试我解雇了,因为它从来没有进入过其他人。但是在目录中我可以看到两条路径,其中一条是存储卡(/storage/3BDD-1A1D)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 2015-06-19
  • 2011-08-07
  • 1970-01-01
相关资源
最近更新 更多