【问题标题】:Obtaining thumbnails for images in android using ContentResolver and Cursor使用 ContentResolver 和 Cursor 在 android 中获取图像的缩略图
【发布时间】:2012-01-23 10:04:03
【问题描述】:

以下是我获取给定图像缩略图的代码。 截至目前,我得到的例外是“光标越界”

我认为这可能是因为我没有在任何地方附加图像 URL。我对在哪里做这件事有点困惑。 所以2个问题: 1.我想获取缩略图的图片URL在哪里使用 2.应该打印列名的for循环除了第一条语句'COLUMN NAMES'之外什么都不打印

        //get the corresponding thumbnail
                String lastImageTakenPath = MyActivity.this.savedInstanceStateVariable.getString("lastImageTaken");
                System.out.println("previous image is "+ lastImageTakenPath);                   

        ContentResolver cr = getContentResolver();
        if(cr != null){


        String[] projection = { MediaStore.Images.Media._ID };  

                    Cursor cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,projection,null,null,null);



        //Cursor cursor = cr.query(lastImageTakenURI, null, null, null, null);
        //Activity.startManagingCursor(cursor);
         if(cursor != null){
        String[] columnNames = cursor.getColumnNames();

                        System.out.println("COLUMN NAMES");
        for(int i=0;i<columnNames.length; i++){
           System.out.println(columnNames[i]);
         }


          /* 1. get the id of the image
                * 2. use this id in the call, getThumbnails on MediaStore.Images.Thumbnails to obtain the 
                            thumbnail
         3.set the imageview's src to this thumbnail */

        int imageID = cursor.getInt( cursor.getColumnIndex(MediaStore.Images.Media._ID) ); 

         Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                       // Get original image ID  
                       String url = uri.toString();
                       int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
        // Get (or create upon demand) the micro thumbnail for the original image.    
          thumbnailLastImage = MediaStore.Images.Thumbnails.getThumbnail(cr, originalImageId, MediaStore.Images.Thumbnails.MICRO_KIND,null);

         thumbnailImage.setImageBitmap(thumbnailLastImage);



          } 
          else{
            System.out.println("Cursor is NULL");
             }
    }
    else{
      Log.d(TAG,"ContentResolver is NULL");
    }

【问题讨论】:

  • 我觉得 managedQuery() 中的 URI 应该是这样的 MediaStore.Images.Media.EXTERNAL_CONTENT_URI 而不是 MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI。

标签: android cursor thumbnails mediastore android-contentresolver


【解决方案1】:

我相信您对 if(cursor != null) 的测试不正确或至少不够充分。如果查询结果没有返回缩略图,那么您仍然会在 cursor.getCount() == 0 处获得一个光标,您可能希望将其用作测试。

【讨论】:

    猜你喜欢
    • 2017-06-22
    • 2014-03-28
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 2015-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多