2014-09-25 71 views
2

我想實現一個簡單的圖像捕獲到我的android應用程序,並返回圖像保存的路徑。我下面從Android文檔樣本,但正如我捕捉圖像,然後按SAVE,我的應用程序崩潰,並顯示以下異常:失敗,意圖結果回到活動,Android圖像捕獲意圖

未能提供結果ResultInfo {誰= NULL,請求= 100,結果= - 1, 數據= NULL}到{活動com.fideli/com.fideli.MainActivity}: 顯示java.lang.NullPointerException

爲什麼數據=空?我試圖尋找一個修復,但沒有一個幫助。有人能幫我一個建議嗎?

謝謝!

MainActivity:

private void takePicture() { 
     // create Intent to take a picture and return control to the calling application 
     Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

     fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image 
     intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name 

     // start the image capture Intent 
     startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE); 

    } 


    /** Create a file Uri for saving an image or video */ 
    private static Uri getOutputMediaFileUri(int type){ 
      return Uri.fromFile(getOutputMediaFile(type)); 
    } 

    /** Create a File for saving an image or video */ 
    private static File getOutputMediaFile(int type){ 
     // To be safe, you should check that the SDCard is mounted 
     // using Environment.getExternalStorageState() before doing this. 

     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES), "MyCameraApp"); 
     // This location works best if you want the created images to be shared 
     // between applications and persist after your app has been uninstalled. 

     // Create the storage directory if it does not exist 
     if (! mediaStorageDir.exists()){ 
      if (! mediaStorageDir.mkdirs()){ 
       Log.d("MyCameraApp", "failed to create directory"); 
       return null; 
      } 
     } 

     // Create a media file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     File mediaFile; 
     if (type == MEDIA_TYPE_IMAGE){ 
      mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
      "IMG_"+ timeStamp + ".jpg"); 
     } else { 
      return null; 
     } 

     return mediaFile; 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     //Bundle bundle = data.getExtras(); 
     //Log.i("TAG", "Image saved to:\n" + bundle); 
     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) { 
      if (resultCode == RESULT_OK) { 
       // Image captured and saved to fileUri specified in the Intent 
       // Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show(); 
       Log.i("TAG", "Image saved to:\n" + data.getData()); 
      } else if (resultCode == RESULT_CANCELED) { 
       // User cancelled the image capture 
      } else { 
       // Image capture failed, advise user 
      } 
      } 
     } 
+0

發佈'MainActivity'的清單條目 – 2014-09-25 22:13:41

回答

1

的圖像被寫入到您的意向額外MediaStore.ACTION_IMAGE_CAPTURE.EXTRA_OUTPUT通過URI。返回到onActivityResult()活動的Intent不包含圖像,也不包含Intentdata字段中的Uri。在onActivityResult()中,您可以從呼叫中返回的文件中獲取圖像getOutputMediaFileUri(int type)

+0

感謝您的建議! – 2014-09-28 07:25:29