3

我正在尝试基于缩略图路径获取图像路径,我已经尝试了来自 android-getting-path-to-image-from-thumbnail 的解决方案,但它基于 gridview 位置,我只是检索特定图像。我还从 SO 中找到了一个示例代码,代码是

private String getImagePathFromThumbPath(String thumbPath)
    {
        String imgPath=null;

//      String[] projection = new String[] {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID};
        String[] imagesDataPath={ MediaStore.Images.Media.DATA ,MediaStore.Images.Media._ID};
            //mResolver.query() requires android API 16
        Cursor thumbnails = mResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imagesDataPath,MediaStore.Images.Thumbnails.DATA+"=?",new String[]{thumbPath}, null, null);

        String[] filePathColumn = {MediaStore.Images.Media.DATA};

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

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

上面的方法稍作修改以适应我的需要,它在 Toasting 时没有返回任何内容,请告诉如何使用缩略图路径检索图像路径?

4

1 回答 1

6

经过长时间和不懈的尝试,解决方案就在这里

1.你需要从缩略图表中的images表中找到图像唯一id,如果你不明白,请查询缩略图提供者( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI ),参考这里,特别是IMAGE_ID

第 1 步是获取retrievedImageId.

retrievedImageId = Long.parseLong(cursor.getString(imageIdInImages));

2.现在使用retrievedImageId获取图像路径,通过再次查询内容提供者,仅这次查询图像媒体提供者(MediaStore.Images.Media.EXTERNAL_CONTENT_URI

String getImagePathFromThumbPath(String thumbPath)
    {
        String imagePath = null;

        if(thumbPath != null )
        {
            String[] columns_to_return = {MediaStore.Images.Thumbnails.IMAGE_ID};
            String where = MediaStore.Images.Thumbnails.DATA+" LIKE ?";
            long retrievedImageId = -1;
            String valuesAre[] = {"%"+thumbPath};
            Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, columns_to_return, where, valuesAre, null);
            if(cursor != null)
            {           
                int imageIdInImages = cursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);

                for (cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()) 
                {
                       //STEP 1 to retrieve image ID
                   retrievedImageId = Long.parseLong(cursor.getString(imageIdInImages));
                }

                if(retrievedImageId != -1)
                {
                    // STEP 2 Now
                    Log.i(TAG, "imageId-" + retrievedImageId);
                    String[] columnsReturn = {MediaStore.Images.Media.DATA};
                    String whereimageId = MediaStore.Images.Media._ID+" LIKE ?";
                    String valuesIs[] = {"%" + retrievedImageId};
                    Cursor mCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columnsReturn, whereimageId, valuesIs, null);
                    int rawDataPath= mCursor.getColumnIndex(MediaStore.Images.Media.DATA);
                    for (mCursor.moveToFirst();!mCursor.isAfterLast(); mCursor.moveToNext()) 
                    {
                        imagePath = mCursor.getString(rawDataPath);
                    }
                }
            }
        }   

        return imagePath;
    }

如果您仍有疑问或错误/异常,请发表评论!

于 2013-04-11T17:06:51.417 回答