【问题标题】:Error in getting filepath from URI in Android在 Android 中从 URI 获取文件路径时出错
【发布时间】:2013-04-13 21:07:51
【问题描述】:

这是一个返回给定 URI 的文件路径的代码。

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    //String[] projection = { MediaColumns.DATA };

    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

这适用于画廊中的图像,因为投影是 MediaStore.Images。如何获取所有其他目录的文件路径?该文件可以是文档类型,并且可以在 sd 卡的任何位置。文件路径应该搜索什么投影?

【问题讨论】:

    标签: java android file uri


    【解决方案1】:

    问题就在这里。它仅检查 Images 中的投影。因此,如果文件的类型不同,您将无法获得路径。 Exception 将被退回。也更好用:

     int column_index = cursor.getColumnIndex(MediaColumns.DATA);
    

    并使用您的 onw try-catch 块。这将防止您的应用崩溃

    【讨论】:

    • 谢谢!我已经解决了。这就是我所做的以及其他一些小调整以使其工作:)
    猜你喜欢
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多