【问题标题】:Android: Get file path from Uri of file in SD cardAndroid:从 SD 卡中文件的 Uri 获取文件路径
【发布时间】:2020-03-26 17:54:48
【问题描述】:

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

方案是“内容”。

代码 sn-p

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
}

【问题讨论】:

  • 为什么有人有权在不解释的情况下标记问题?问责制是什么?
  • 同样的原因你可以在没有解释的情况下投票。为什么你不在安卓聊天和一些安卓论坛上发布你的问题呢?
  • 您的问题质量非常低,我曾投过赞成票,但现在将其删除。没有上下文。您谈论的某些文件似乎跳到了某些光标/数据库API?注意:我没有对你的问题投反对票。以防万一你找人抱怨:-) youtube.com/watch?v=LXrTUuTJRn0
  • 这不是数据库查询。他正在使用内容解析器。请注意,我建议查看内容提供商。你的解析器代码对我来说是正确的。您可能调用不正确,或者它可能不像您想象的那样工作。
  • @KrishnanVS 你在尝试什么 API 级别?因为从 API 级别 29 开始,您无法直接访问外部存储。您需要使用存储访问框架。有关更多信息,请参阅:developer.android.com/training/data-storage/shared/…

标签: android sd-card android-external-storage


【解决方案1】:

试试这段代码,我正在使用这个函数从 uri 获取真实路径:

private String getRealPathFromURI(Context mContext, Uri contentURI) {
    String result = null;
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Video.Media.DATA};
        ContentResolver mContentResolver = mContext.getContentResolver();
        String mime = mContentResolver.getType(contentURI);
        cursor = mContentResolver.query(contentURI, proj, null, null, null);
        if (cursor == null) {
            return null;
        } else {
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
            if (column_index > -1)
                result = cursor.getString(column_index);
            cursor.close();
        }
    } catch (Exception e) {
        return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return result;
}

它会返回一个字符串,它是你文件的真实路径。

希望对你有所帮助。

【讨论】:

  • 米兰,感谢您的回复。但是,正如我在问题中所说,游标没有获取该列“DATA”。这是奇怪的部分。
  • 尝试一下这个功能,然后重播发生的事情就可以了
  • 结果为空,column_index 为 -1。 uri.getAuthority 返回“com.lenovo.FileBrowser.FileProvider”。方案是“内容。
  • 更新:当我尝试与另一个文件管理器应用程序共享时,我将权限作为“媒体”并获得了路径。库存文件管理器应用程序导致了此问题。
  • 默认文件管理器是导致此问题的原因。我尝试了另一个,效果很好。感谢您抽出宝贵时间提供帮助。
【解决方案2】:

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

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

使用contentResolver 获取FileDescriptor 以传递给实用程序

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

此常量在 API 级别 29 中已弃用。 应用程序可能没有直接访问此路径的文件系统权限。应用程序不应尝试直接打开此路径,而应使用 ContentResolver#openFileDescriptor(Uri, String) 来获得访问权限。

【讨论】:

    【解决方案3】:

    默认文件管理器是导致此问题的原因 - 未返回“DATA”列并将授权返回为“com.lenovo.FileBrowser.FileProvider”。当我使用另一个文件管理器时,权限是“媒体”。在运行 ContenResolver 时还使用“DATA”获取路径列。感谢大家努力提供帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 2011-03-01
      相关资源
      最近更新 更多