Android 获取图库图片 Uri 路径 [英] Android get Gallery image Uri path

查看:62
本文介绍了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);
}

这篇关于Android 获取图库图片 Uri 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆