2012-05-11 63 views
3

我有一個應用程序處理從畫廊中選擇的圖片。但對ICS崩潰這樣的堆棧跟蹤:未能提供畫廊的結果

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, Intent { dat=content://com.google.android.gallery3d.provider/picasa/item/5629933319233370946 }} to activity {com.myexample/com.myexample.activities.UserProfileActivity}: java.lang.NullPointerException 
at android.app.ActivityThread.deliverResults(ActivityThread.java:2980) 
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023) 
at android.app.ActivityThread.access$1100(ActivityThread.java:123) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4424) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 
at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException 
at java.io.File.fixSlashes(File.java:185) 
at java.io.File.(File.java:134) 
at com.jettaxi.activities.UserProfileActivity.onActivityResult(SourceFile:316) 
at android.app.Activity.dispatchActivityResult(Activity.java:4649) 
at android.app.ActivityThread.deliverResults(ActivityThread.java:2976) 
... 11 more 

我發現關於這些漏洞的一些信息,但我能避免這種情況又如何呢?

編輯: 我的代碼:

private void getImageFromGallery() { 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
     startActivityForResult(galleryIntent, GALLERY_PIC_REQUEST); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == CAMERA_PIC_REQUEST) { 
      try { 
       captureFile = new File(getCaptureFilePath()); 
       captureUri = Uri.fromFile(captureFile); 

       Bitmap scaledBitmap = decodeFileAndResize(captureFile); 
       saveResizedAndCompressedBitmap(scaledBitmap); 

       Bitmap rotatedBitmap = convertToRotatedBitmap(scaledBitmap); 
       driverPhoto.setImageBitmap(rotatedBitmap); 

       if (rotatedBitmap != scaledBitmap) { 
        scaledBitmap.recycle(); 
        scaledBitmap = null; 
        System.gc(); 
       } 
      } catch (IOException e) { 
       BugSenseHandler.log(TAG, e); 
      } 
     } else if (requestCode == GALLERY_PIC_REQUEST) { 
      try { 
       captureFile = new File(getCaptureFilePath()); 
       captureUri = Uri.fromFile(captureFile); 

       Uri chosenUri = data.getData(); 

       String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

       Cursor cursor = getContentResolver().query(chosenUri, filePathColumn, null, null, null); 
       cursor.moveToFirst(); 

       int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       String filePath = cursor.getString(columnIndex); 
       cursor.close(); 

       File chosenFile = new File(filePath); 

       Bitmap selectedImageBitmap = decodeFileAndResize(chosenFile); 
       saveResizedAndCompressedBitmap(selectedImageBitmap); 
       driverPhoto.setImageBitmap(selectedImageBitmap); 
      } catch (IOException ie) { 
       BugSenseHandler.log(TAG, ie); 
      } 
     } 
    } 
} 

回答

10

這可能通過阻止列出不是在SD卡上的項目幫助:

galleryIntent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); 
+0

任何想法爲什麼你不能得到一個URI不是在SD卡上的項目? – speedynomads

+0

請注意,該字段需要API級別11。 – Sandy

0

堆棧跟蹤說,圖像是從Picasa相冊。所以你在SD卡上沒有文件。在這種情況下,使用getContentResolver().openInputStream(intent.getData())加載圖像。

+0

圖片SD卡的存在,因爲它出現在畫廊,以及一些數量的設備出現這個問題,根本沒有 – skayred

+0

Picasa圖像不需要在SD卡上。只要看看Gallery3D的源代碼。所以你可能會得到一個指向在線圖像的URI,但是你要求內容提供商提供一個不存在的文件路徑。 – Renard

+0

好吧,那麼我們如何檢查或防止呢? – ChuckKelly