【问题标题】:Android getting path to image from Thumbnail?Android从缩略图获取图像的路径?
【发布时间】:2012-03-05 23:28:48
【问题描述】:

我正在尝试为我目前在市场上的应用程序发布关键更新。我需要查询 MediaStore 中的缩略图,并将缩略图加载到 GridView 中。到目前为止一切顺利,现在我只需要根据我所拥有的(即缩略图的路径)来获取用户外部存储上实际全尺寸图像的路径。每当用户单击缩略图、共享、查看、移动和删除时,我都需要能够执行 4 个操作……但我无法仅使用缩略图路径来执行任何这些操作,我已经尝试了所有操作,但没有工作 :/。下面是我的实现的一个 sn-p,对此的任何帮助或指导将不胜感激!

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

    int columnIndex = 0;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);

    if(cursor != null){
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToPosition(position);
        imagePath = cursor.getString(columnIndex);

        FileInputStream is = null;
        BufferedInputStream bis = null;

        try{
            is = new FileInputStream(new File(imagePath));
            bis = new BufferedInputStream(is);
            Bitmap bitmap = BitmapFactory.decodeStream(bis);
            useThisBitmap = Bitmap.createScaledBitmap(bitmap, parent.getWidth(), parent.getHeight(), true);

            bitmap.recycle();
        }catch(Exception e){
            //Try to recover
        }
        finally{
            try{
                if(bis != null){
                    bis.close();
                }
                if(is != null){
                    is.close();
                }
                cursor.close();
                projection = null;
            }catch(Exception e){

            }
        }
    }
    Toast.makeText(this, "  " + imagePath, Toast.LENGTH_SHORT).show();

到目前为止,我已经尝试了所有使用 imagePath 成员的方法,但这会导致抛出异常或其他错误。有什么建议吗?

【问题讨论】:

    标签: android gridview path thumbnails


    【解决方案1】:

    我找到了解决方案。在我查询 MediaStore 内容解析器并抓取缩略图之前(反正不是我需要的)。这种优化允许我以完全相同的方式搜索 MediaStore,但这次我搜索的是 ACTUAL 图像,并且返回的是缩略图 MICRO_KIND。然后将图像缓存在位图数组中,然后用于填充 GridView。我现在能够获得设备上实际全尺寸图像的路径。如果我需要使用该路径创建 URI 实例或文件,现在我可以轻松完成。对于遇到此问题或类似问题Android getting path to image from Thumbnail? 的任何其他人,下面是解决方案代码。顺便说一句,我在 AsyncTask 中执行此操作,并在显示设备上的所有图像后显示/关闭加载对话框。

      /*----------------------------ASYNC TASK TO LOAD THE    PHOTOS--------------------------------------------------------*/
    
    public class LoadPhotos extends AsyncTask<Object, Object, Object>{
    
        @Override
        protected Object doInBackground(Object... params) {
            final String[] columns = { MediaStore.Images.Media._ID };
            final String orderBy = MediaStore.Images.Media._ID;
    
            Cursor imagecursor = managedQuery(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                    null, orderBy);
    
            int image_column_index = imagecursor
                    .getColumnIndex(MediaStore.Images.Media._ID);
    
            AllPhotosActivity.count = imagecursor.getCount();
            AllPhotosActivity.windows = new Bitmap[AllPhotosActivity.count];
    
            for (int i = 0; i < AllPhotosActivity.count; i++) {
                imagecursor.moveToPosition(i);
                //i = index;
                int id = imagecursor.getInt(image_column_index);
                windows[i] = MediaStore.Images.Thumbnails.getThumbnail(
                        getApplicationContext().getContentResolver(), id,
                        MediaStore.Images.Thumbnails.MICRO_KIND, null);
            }
    
            imagecursor.close();
            return null;
    
        }
    
        @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pd.dismiss();
            imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
    
        }
    
        @Override
        protected void onProgressUpdate(Object... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }
    
    
    
    }
    

    这是我的 onItemClick() "filename" 成员用于创建 URI 实例或您需要对图像执行的任何操作。

    public void onItemClick(AdapterView<?> arg0, android.view.View v,
                    int position, long id) {
                String[] columns = { MediaStore.Images.Media.DATA,
                        MediaStore.Images.Media._ID };
                Cursor actualimagecursor = managedQuery(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                        null, null, null);
    
                final int dataColumnIndex = actualimagecursor
                        .getColumnIndex(MediaStore.Images.Media.DATA);
                final int idColumIndex = actualimagecursor
                        .getColumnIndex(MediaStore.Images.Media._ID);
    
                actualimagecursor.moveToPosition(position);
    
                filename = actualimagecursor.getString(dataColumnIndex);
                final long imageId = actualimagecursor.getLong(idColumIndex);
    

    【讨论】:

    • 非常感谢您的代码,但它不是给出所选图像的精确路径。当正在选择第一个图像时,它为第2图像的路径。 span>
    【解决方案2】:
    Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
    

    使用此代码,我认为它会起作用。基本上缩略图和原始图像的 id 相同,因此缩略图和图像的位置将相同。

    【讨论】:

    • 我不确定我理解你的意思。如您所见,我已经在使用您发布的代码。请提供更详细的解决方案。
    • 我试过你的代码。它不起作用,因为我从 MediaStore 获得随机 Uris,而不是全尺寸图像的 Uris
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2013-08-13
    • 2017-03-10
    • 2014-03-28
    • 2011-07-29
    • 2013-08-12
    • 1970-01-01
    相关资源
    最近更新 更多