从 Android 缩略图查询中获取图片

3

我有这段代码(在某个地方找到的):

    public static List<MyImages> getImages(Activity context) {
    List<MyImages> lst = new ArrayList<MyImages>();
    Cursor cursor = getCameraThumbImages(context);
    if (cursor != null) {
        int columnIndex = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int columnIndexPath = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
        int columnIndexImagePath = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID);
        int count = cursor.getCount();
        for (int i = 0; i < count; i++) {
            cursor.moveToPosition(i);

            int imageID = cursor.getInt(columnIndex);
            String path = cursor.getString(columnIndexPath);
            Uri imgThmbPath = Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
                            + imageID);
            String hope = cursor.getString(columnIndexImagePath);
            MyImages p2p = new MyImages(path, "" + imageID);
            lst.add(p2p);
        }
    }

    return lst;
}

这段代码使我能够访问手机上的图像缩略图。问题是我不知道如何从中获取原始图像路径。
问题是:给定缩略图(或游标),我该如何获取原始图像路径?
1个回答

3
在缩略图中,您有MediaStore.Images.Thumbnails.IMAGE_ID字段,可以从中获取相关的图像ID。然后查询MediaStore.Images.Media并从MediaStore.Images.Media.DATA字段获取您的照片路径。 编辑
// First request thumbnails what you want
String[] projection = new String[] {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID};
Cursor thumbnails = contentResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);

// Then walk thru result and obtain imageId from records
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {
    String imageId = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.IMAGE_ID));

    // Request image related to this thumbnail 
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor images = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, MediaStore.Images.Media._ID + "=?", new String[] {imageId}, null);

    if (cursor != null && cursor.moveToFirst()) {
        // Your file-path will be here
        String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
    }

}

//Of course you need to restrict queries using selection and selection args params and get only rows that you really need

谢谢您的评论。您能否给我提供实现此功能的代码(或代码链接)?我不太明白如何进行查询。再次感谢。 - Zelter Ady
像往常一样使用内容提供程序进行查询。 - Dmitriy Tarasov
谢谢。我会检查你的代码并告诉你是否解决了我的问题。 - Zelter Ady

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接