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的遊標​​,你可能想用它作爲你的測試。