2011-12-04 49 views
4

在我們的代碼,我們使用getPhoto方法,看起來像這樣:的Android ACTION_IMAGE_CAPTURE有時不叫onActivityResult

public void getPhoto(View view) { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    captureFile = new File(getCaptureFilePath()); 
    captureUri = Uri.fromFile(captureFile); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri); 

    startActivityForResult(intent, CAPTURE_IMAGE); 
} 

而且onActivityResult:

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

    Log.w(TAG, "Came"); 

    if (resultCode == RESULT_OK) { 
     if (requestCode == CAPTURE_IMAGE) { 
      try { 
       captureFile = new File(getCaptureFilePath()); 
       captureUri = Uri.fromFile(captureFile); 

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

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

       Log.w(TAG, "Before recycle"); 

       if (rotatedBitmap != scaledBitmap) { 
        scaledBitmap.recycle(); 
        scaledBitmap = null; 
        System.gc(); 
       } 

       Log.w(TAG, "After recycle"); 
      } catch (IOException e) { 
       BugSenseHandler.log(TAG, e); 
      } 
     } 
    } 
} 

有時候,當我按 '確定', onActivityResult不叫(Came沒有寫)。我的代碼有什麼問題?

編輯:12-04 12:43:36.040: INFO/WindowManager(145): WIN DEATH: Window{40839990 com.skalar/com.skalar.activities.RegisterActivity paused=false}onActivityResult未調用時出現在代碼中。

+0

請參閱http://stackoverflow.com/a/20433752/192373 –

回答

4

是否有可能您的活動被殺死,這是onActivityResult沒有得到執行的原因?當攝像頭Intent返回時,通常onActivityResult後跟onResume將被執行。在你的onPause和onResume方法中放入一條日誌語句,並檢查執行順序。如果你使用Android noHistory =「true」標記清單中 :

+0

當Camera顯示時(如預期的那樣)它暫停,然後調用WIN DEATH,然後調用我的Starter Activity,如清單 – skayred

+0

中指定的那樣。查看堆棧跟蹤並查找哪個代碼導致WIN DEATH很有用。看到這一些想法http://stackoverflow.com/questions/6256730/how-to-prevent-my-android-app-from-force-closing – user994886

+0

是的,它正在被殺死。現在如何克服它? – Yar

2
I faced same problem , once check have you put any tag related to History ? 

不要把機器人noHistory在裏面體現你的活動=「true」時,它會從 堆棧去除停止後。注意:如果你正在使用標籤活動,那麼你也不應該使用android:noHistory =「true」 或者簡單地把android:noHistory =「false」放在清單中的activity中。

可能是我的解釋是錯誤的,但我得到的解決方案。根據

0

https://groups.google.com/forum/#!topic/android-developers/aihsOU8uAzU

The method that takes care of the capture only returns correctly 
(using the finnish() method, I think) if the picture can be saved 
correctly in the file specified by that URI. If there's an exception 
(such as an IO exception) it will be captured and nothing will be 
done, so you'll never know. I believe that 'myTestFile' is a file that 
can only be read/written within your own application and that's why 
image_capture can't write to it. 

所以問題是

captureFile = new File(getCaptureFilePath()); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri); 

你可以通過設置captureFile此方法的返回值解決這個問題:

private File createImageFile() throws IOException { 
     // Create an image file name 
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
     String imageFileName = "JPEG_" + timeStamp + "_"; 
     File directory = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "your subdirectory for Picture directory"); 
     if(!directory.exists() && !directory.mkdir()) 
      return null; 
     File image = File.createTempFile(
       imageFileName, /* prefix */ 
       ".jpg",   /* suffix */ 
       directory  /* directory */ 
     ); 

     // Save a file: path for use with ACTION_VIEW intents 
     return image; 
    }