如何从一个图像缩略图路径的ImagePath? [英] How to get imagepath from thumbnail path of a image?

查看:176
本文介绍了如何从一个图像缩略图路径的ImagePath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于thumbail道路上获得的ImagePath,我已经尝试从溶液
<一href=\"http://stackoverflow.com/questions/9279553/android-getting-path-to-image-from-thumbnail\">android-getting-path-to-image-from-thumbnail,但它是基于gridview的位置,我只检索特定的图像。另外我发现一个样品code从如此,code是

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

上面的方法有点修改,以适合我的需要,它在祝酒没有返回值,请告知如何使用缩略图路径检索的ImagePath?

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?

推荐答案

在很长的时间和不懈的尝试,解决的办法是在这里

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

1 您需要找到图片ID是从图像中表图像唯一的ID在缩略图表,查询到缩略图提供商(<一个href=\"http://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html#EXTERNAL_CONTENT_URI\"相对=nofollow> MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI ),如果你不理解,是指的这里,特别是<一个href=\"http://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html#IMAGE_ID\"相对=nofollow> IMAGE_ID ,

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,

第1步是要获得 reterievedImageId

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

2 立即使用 reterievedImageId 获取图像的路径,再次quering内容提供商,只是这一次查询图像媒体提供商( <一href=\"http://developer.android.com/reference/android/provider/MediaStore.Images.Media.html#EXTERNAL_CONTENT_URI\"相对=nofollow> MediaStore.Images.Media.EXTERNAL_CONTENT_URI )

2. Now using the reterievedImageId get the image path, by again quering 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 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;
    }

如果您还有疑问或错误/异常,留下评论!

这篇关于如何从一个图像缩略图路径的ImagePath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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