Android 获取图库图片 Uri 路径

问题描述:

在一个活动中,我可以从图库中选择一个图像,我需要它的 Uri 路径(在日志中,我的测试图像的 Uri 路径是 /content:/media/external/images/media/1).

In an Activity, I can choose an image from the Gallery, and I need its Uri path (in the log, the Uri path for my test image is /content:/media/external/images/media/1).

我收到了这个错误:

08-04 02:14:21.912: DEBUG/PHOTOUPLOADER(576): java.io.FileNotFoundException: /content:/media/external/images/media/1 (No such file or directory)
08-04 02:14:32.124: WARN/System.err(576): java.io.FileNotFoundException: /content:/media/external/images/media/1 (No such file or directory)

这是文件路径的正确格式吗?或者我应该让它像 sdcard...image.png 之类的东西?

Is this the correct format of a file path? Or should I make it to be something like sdcard...image.png?

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    startManagingCursor(cursor);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}