2012-04-19 90 views
0

這裏使用遊標時是代碼:NullPointerException異常通過返回Android攝像頭意圖

//做的「三星」的方式

  // Describe the columns you'd like to have returned. 
      // Selecting from the Thumbnails location gives you both the 
      // Thumbnail Image ID, as well as the original image ID 
      String[] projection = { 
        MediaStore.Images.Thumbnails._ID, // The columns we 
                 // want 
        MediaStore.Images.Thumbnails.IMAGE_ID, 
        MediaStore.Images.Thumbnails.KIND, 
        MediaStore.Images.Thumbnails.DATA }; 
      String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select 
                      // only 
                      // mini's 
        MediaStore.Images.Thumbnails.MINI_KIND; 

      String sort = MediaStore.Images.Thumbnails._ID + " DESC"; 

      // At the moment, this is a bit of a hack, as I'm returning 
      // ALL images, and just taking the latest one. There is a 
      // better way to narrow this down I think with a WHERE 
      // clause which is currently the selection variable 
      Cursor myCursor = this.managedQuery(
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
        projection, selection, null, sort); 
      /* 
      CursorLoader loader = new CursorLoader(getApplicationContext(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
        projection, selection, null, sort); 
      Cursor myCursor = loader.loadInBackground(); 
      */ 

      long imageId = 0l; 
      long thumbnailImageId = 0l; 
      String thumbnailPath = ""; 

      try { 
       myCursor.moveToFirst(); 


       **imageId = myCursor 
         .getLong(myCursor 
           .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));** 
       thumbnailImageId = myCursor 
         .getLong(myCursor 
           .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID)); 
       thumbnailPath = myCursor 
         .getString(myCursor 
           .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA)); 
      } finally { 
       myCursor.close(); 
      } 
      Uri uriThumbnailImage = Uri.withAppendedPath(
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
        String.valueOf(thumbnailImageId)); 
      // convert to a bytestream -- hack to convert URI to string 

上有星號標記的線NPE:

04-19 03:33:23.180: E/AndroidRuntime(29375): java.lang.RuntimeException: Failure  delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity  {kaana.app/kaana.app.InEventStartActivity}:  android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 

我使用此代碼從另一個來源,專門爲三星手機(我使用Galaxy Nexus)設計此修復程序。任何想法,爲什麼這不工作?

+0

你的'myCursor'沒有任何值,它的大小是零檢查它 – 2012-04-19 07:47:19

+0

我知道,但爲什麼它是0,當我只是成功地拍了一張照片? – maxko87 2012-04-19 15:13:27

+0

你有沒有解決這個問題?我有同樣的問題。 – 2012-12-10 23:53:38

回答

0

您是否在清單文件中提供了許可相機?

+0

是的,我有。其他型號不存在這個問題。 – maxko87 2012-05-10 23:20:43

+0

我也有同樣的問題。對於我所做的是當我捕捉圖像時,我將uri與相機意圖一起發送。然後根據活動結果將該uri調用並獲取圖像。如果你想我會發布代碼。 – SKT 2012-05-14 09:19:28

+0

可以ü請在這裏分享示例代碼。 – Avinazz 2013-03-11 11:25:55