Android:从SD卡中文件的Uri获取文件路径

标签 android sd-card android-external-storage

我们正在尝试获取保存在 SD 卡中的文件的路径,以传递给实用程序。然而,当我们使用 URI 打开游标时,只有两列被提取,路径(_data 列)丢失了。请让我知道如何获取路径。该查询适用于内部存储中的任何文件。

方案是“内容”。

代码 fragment

cursor = context.getContentResolver().query(uri, null, null, null, null);
if (null != cursor && cursor.moveToFirst())
{
    int testcolCount = cursor.getColumnCount();
    for (int i = 0; i < testcolCount; i++)
    {
        String colName = cursor.getColumnName(i);
        String colValue = cursor.getString(i);
        System.out.println("" + i + ": " + colName + ":" + colValue + "\n");
    }

    //This prints only two columns, _display_name and _size
}

最佳答案

根本不要尝试获取路径,Android 10 及更高版本的应用程序私有(private)目录之外的文件路径毫无用处,因为您无权访问它们。

参见 https://developer.android.com/training/data-storage#scoped-storage

使用 contentResolver 获取 FileDescriptor 以传递给 Utility

注释来自 https://developer.android.com/reference/android/provider/MediaStore.MediaColumns.html#DATA

This constant was deprecated in API level 29. Apps may not have filesystem permissions to directly access this path. Instead of trying to open this path directly, apps should use ContentResolver#openFileDescriptor(Uri, String) to gain access.

关于Android:从SD卡中文件的Uri获取文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59133506/

相关文章:

java - 更新到 Android 4.4 KitKat 后无法找到 Movies 文件夹

android - 打开具有特定相册/文件夹的默认照片应用程序

android - 为什么不需要 READ_EXTERNAL_STORAGE 权限请求来从外部存储读取文件?

android - OnPageChangeListener alpha 淡入淡出

android - 无法使用 C++ 在 OpenGL ES 1.x 中绘制加载模型

java - 无法从 AsyncTask 更新 UI

android - 从市场安装时, list 中的 preferexternal 不起作用

android - 每行带有按钮的 ListFragment

android - 制造商的可移动存储(外部)SD卡路径

filesystems - 如何使用 SD 卡以 48 ksamples/s 的速度记录 16 位数据?