使用ContentResolver和游标在android中获取图像缩略图

使用ContentResolver和游标在android中获取图像缩略图,android,cursor,thumbnails,mediastore,android-contentresolver,Android,Cursor,Thumbnails,Mediastore,Android Contentresolver,下面是我获取给定图像缩略图的代码。 到目前为止,我得到的例外是“光标超出范围” 我想这可能是因为我没有在任何地方添加图像URL。我有点不知道该在哪里做。 因此,有两个问题: 1.在哪里使用要获取其缩略图的图像URL 2.应该打印列名的for循环只打印第一条语句“column names” 我相信你对ifcursor的测试null不正确或至少不充分。如果查询结果没有返回缩略图,那么您仍然会得到一个游标,其中cursor.getCount==0您可能希望将其用作测试。我觉得managedQuery中

下面是我获取给定图像缩略图的代码。 到目前为止,我得到的例外是“光标超出范围”

我想这可能是因为我没有在任何地方添加图像URL。我有点不知道该在哪里做。 因此,有两个问题: 1.在哪里使用要获取其缩略图的图像URL 2.应该打印列名的for循环只打印第一条语句“column names”


我相信你对ifcursor的测试null不正确或至少不充分。如果查询结果没有返回缩略图,那么您仍然会得到一个游标,其中cursor.getCount==0您可能希望将其用作测试。

我觉得managedQuery中的URI应该类似于MediaStore.Images.Media.EXTERNAL内容URI,而不是MediaStore.Images.thumbnails.EXTERNAL内容URI。
        //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");
    }