如何从图像的缩略图路径获取imagepath?

时间:2013-03-21 13:27:23

标签: android

我正在尝试基于拇指路径获取imagepath,我已经尝试过解决方案了 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上没有返回任何内容,请告诉我如何使用缩略图路径检索图像路径?

1 个答案:

答案 0 :(得分:6)

经过很长时间的不懈努力,解决方案就在这里

1。您需要在缩略图表格中的图片表中找到图像唯一ID的图片ID,如果您不理解,请查询缩略图提供者(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI),参考here,特别是IMAGE_ID

第1步是获取reterievedImageId

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

2。现在使用reterievedImageId获取图片路径,再次查询内容提供商,此时只查询图片媒体提供商(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 reterievedImageId=-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
                   reterievedImageId=Long.parseLong(cursor.getString(imageIdInImages));
                }
                if(reterievedImageId!=-1)
                {
                       //STEP 2 Now
                    Log.i(TAG, "imageId-"+reterievedImageId);
                    String[] columnsReturn={MediaStore.Images.Media.DATA};
                    String whereimageId=MediaStore.Images.Media._ID+" LIKE ?";
                    String valuesIs[]={"%"+reterievedImageId};
                    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;
    }

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