2013-04-03 92 views
1

我是Android開發的小菜鳥,我無法返回相機拍攝的圖像。當我垂直拍攝照片/肖像時,我的應用運行正常,但是當水平拍攝照片/橫向時,應用崩潰時出現錯誤,表示失敗,結果ResultInfo {who = null,request = 1,result = -1,data = null }。我調整了我的輸出圖片的高度&,但沒有成功。任何幫助是極大的讚賞。爲什麼我得到失敗提供結果ResultInfo使用相機?

我的代碼

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    if (resultCode == RESULT_OK) { 
     //check if we are returning from picture selection 
     if (requestCode == PICKER) { 

      //the returned picture URI 
    //  Uri pickedUri = data.getData(); 

      //declare the bitmap 
      Bitmap pic = null; 
      //declare the path string 
      String imgPath = ""; 

      //retrieve the string using media data 
      String[] medData = { MediaStore.Images.Media.DATA }; 
      //query the data 
      Cursor picCursor = managedQuery(outputFileUri, medData, null, null, null); 
      if(picCursor!=null) 
      { 
       //get the path string 
       int index = picCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
       picCursor.moveToFirst(); 
       imgPath = picCursor.getString(index); 
      } 
      else 
       imgPath = outputFileUri.getPath(); 

      //if and else handle both choosing from gallery and from file manager 

      //if we have a new URI attempt to decode the image bitmap 
      if(outputFileUri!=null) { 

       //set the width and height we want to use as maximum display 
       int targetWidth = 600; 
       int targetHeight = 400; 

       //sample the incoming image to save on memory resources 

       //create bitmap options to calculate and use sample size 
       BitmapFactory.Options bmpOptions = new BitmapFactory.Options(); 

       //first decode image dimensions only - not the image bitmap itself 
       bmpOptions.inJustDecodeBounds = true; 
       BitmapFactory.decodeFile(imgPath, bmpOptions); 

       //work out what the sample size should be 

       //image width and height before sampling 
       int currHeight = bmpOptions.outHeight; 
       int currWidth = bmpOptions.outWidth; 

       //variable to store new sample size 
       int sampleSize = 1; 

       //calculate the sample size if the existing size is larger than target size 
       if (currHeight>targetHeight || currWidth>targetWidth) 
       { 
        //use either width or height 
        if (currWidth>currHeight) 
         sampleSize = Math.round((float)currHeight/(float)targetHeight); 
        else 
         sampleSize = Math.round((float)currWidth/(float)targetWidth); 
       } 
       //use the new sample size 
       bmpOptions.inSampleSize = sampleSize; 

       //now decode the bitmap using sample options 
       bmpOptions.inJustDecodeBounds = false; 

       //get the file as a bitmap 
       pic = BitmapFactory.decodeFile(imgPath, bmpOptions); 

       if(currentPic<=10){      
        //pass bitmap to ImageAdapter to add to array 
        imgAdapt.addPic(pic); 
        currentPic++; 
       } 

       //redraw the gallery thumbnails to reflect the new addition 
       picGallery.setAdapter(imgAdapt); 

       //display the newly selected image at larger size 
       picView.setImageBitmap(pic); 
       //scale options 
       picView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      } 
     } 
    } 
    //superclass method 
    super.onActivityResult(requestCode, resultCode, data); 
} 

logcat的

04-03 08:26:46.113: E/AndroidRuntime(15318): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.pickenssurvey/com.example.pickenssurvey.PictureGallery}: java.lang.NullPointerException 
04-03 08:26:46.113: E/AndroidRuntime(15318): at android.app.ActivityThread.deliverResults(ActivityThread.java:3179) 
04-03 08:26:46.113: E/AndroidRuntime(15318): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2534) 
04-03 08:26:46.113: E/AndroidRuntime(15318): ... 13 more 
04-03 08:26:46.113: E/AndroidRuntime(15318): Caused by: java.lang.NullPointerException 
04-03 08:26:46.113: E/AndroidRuntime(15318): at android.content.ContentResolver.acquireProvider(ContentResolver.java:913) 
04-03 08:26:46.113: E/AndroidRuntime(15318): at android.content.ContentResolver.query(ContentResolver.java:305) 
04-03 08:26:46.113: E/AndroidRuntime(15318): at android.app.Activity.managedQuery(Activity.java:1742) 
04-03 08:26:46.113: E/AndroidRuntime(15318): at com.example.pickenssurvey.PictureGallery.onActivityResult(PictureGallery.java:241) 
04-03 08:26:46.113: E/AndroidRuntime(15318): at android.app.Activity.dispatchActivityResult(Activity.java:4723) 
04-03 08:26:46.113: E/AndroidRuntime(15318): at android.app.ActivityThread.deliverResults(ActivityThread.java:3175) 

回答

2

當屏幕方向的變化,你的活動被破壞,並在新的方向重新創建。因此,任何在Activity生命週期中獲得引用的變量將不再引用任何內容,如果您嘗試訪問它們引用的對象而不重新賦值它們,則會得到NullPointerException。

onSaveInstanceState()方法用於在配置更改之間保存臨時數據。這將創建一個Bundle,並在Activity再次啓動時傳遞給onCreate()。

沒有任何代碼,我不知道這是你的問題,但它值得一看。

請參閱http://developer.android.com/reference/android/app/Activity.html#ConfigurationChanges瞭解更多信息(以及比我提供的更準確的信息,毫無疑問)。

不是我從其他崗位要支持縱向和橫向視圖在您的應用程序

1

做複製?如果不是,可以通過防止方向改變來解決問題。把你的onCreate下面的代碼:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

它仍然將有可能改變方向在相機意圖。

0

我之前遇到過這個問題,沒有看到符合我的原因的答案。

的問題是:

  1. 攝像頭接管
  2. 旋轉該裝置,而在攝像模式下
  3. 拍攝照片,並確認它
  4. 圖片被髮送回活動,但您的活動/片段獲取 銷燬並重新創建屏幕方向更改 ,包括存儲您的輸出URI的 變量文件/字符串

解決方案:

  1. 存儲在你的onSaveInstanceState方法包含被用於重新創建活動
  2. 檢索從包文件中onRestoreInstanceState活性和onCreateView爲片段
  3. 束輸出URI
相關問題