1

以下是我获取给定图像缩略图的代码。 截至目前,我得到的例外是'光标越界'使用ContentResolver和Cursor获取Android中图像的缩略图

我认为这可能是因为我没有在任何地方追加图像URL。我对于在哪里做到这一点有点困惑。 所以2个问题:1。 我在哪里可以使用图像的URL,其中我想获得一个缩略图 2. for循环这是应该打印的列名打印只按第一条语句“列名”

 //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"); 
    } 

+0

我觉得managedQuery()中的URI应该像这样MediaStore.Images.Media.EXTERNAL_CONTENT_URI而不是MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI。 – Vamsi 2012-01-04 06:29:34

回答

-2

我相信你对if(cursor!= null)的测试不正确或者至少不够。如果查询的结果没有返回缩略图,那么你仍然会得到一个cursor.getCount()== 0的游标​​,你可能想用它作为你的测试。