How to get imagepath from thumbnail path of a imag

2020-07-22 17:17发布

I am trying to get imagepath based on thumbail path , i have already tried solution from android-getting-path-to-image-from-thumbnail, but it is based on gridview position, i am only retrieving specific images. Also i found one sample code from SO, the code is

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;
    }

The above method is bit modified to suit my needs and it returns nothing on Toasting, please tell how to retrieve imagepath using thumbnail path?

标签: android
1条回答
疯言疯语
2楼-- · 2020-07-22 18:07

After a long time and relentless try, the solution is here

1. You need to find the image id which is image unique id from images table in thumbnail table, query to thumbnail provider (MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI) if you do not understand it, refer here , specifically IMAGE_ID,

Step 1 is to get retrievedImageId.

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

2. Now using the retrievedImageId get the image path, by again querying the content provider, only this time query the Images media provider (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;
    }

If you still have doubt or error/exception, leave comment!

查看更多
登录 后发表回答