1

我正在使用相機的應用程序,我想默認的攝像頭,一旦應用程序被打開打開。我目前在我的主要活動的onCreate方法中開始我的圖像捕獲意圖。這有時候工作得很好,但有時候相機意圖會連續三次啓動。意圖開始的onCreate多次調用

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mImageView = (ImageView) findViewById(R.id.imageView1); 
     mImageBitmap = null; 

     Button picBtn = (Button) findViewById(R.id.pictureButton); 
     setBtnListenerOrDisable(
       picBtn, 
       mTakePicOnClickListener, 
       MediaStore.ACTION_IMAGE_CAPTURE 
     ); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { 
      mAlbumStorageDirFactory = new FroyoAlbumDirFactory(); 
     } else { 
      mAlbumStorageDirFactory = new BaseAlbumDirFactory(); 
     } 
     dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B); 
    } 

private void dispatchTakePictureIntent(int actionCode) { 

     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
     File f; 
     try { 
      f = setUpPhotoFile(); 
      mCurrentPhotoPath = f.getAbsolutePath(); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      mCurrentPhotoPath = null; 
     } 

     startActivityForResult(takePictureIntent, actionCode); 
    } 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
      handleBigCameraPhoto(); 
     } 
    } 

private void handleBigCameraPhoto() { 

     if (mCurrentPhotoPath != null) { 
      setPic(); 
      galleryAddPic(); 
      lastPhotoPath = mCurrentPhotoPath; 
      mCurrentPhotoPath = null; 
     } 

    } 

private void setPic() { 

     /* There isn't enough memory to open up more than a couple camera photos */ 
     /* So pre-scale the target bitmap into which the file is decoded */ 

     /* Get the size of the ImageView */ 
     int targetW = mImageView.getWidth(); 
     int targetH = mImageView.getHeight(); 

     /* Get the size of the image */ 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
     int photoW = bmOptions.outWidth; 
     int photoH = bmOptions.outHeight; 

     /* Figure out which way needs to be reduced less */ 
     int scaleFactor = 1; 
     if ((targetW > 0) || (targetH > 0)) { 
      scaleFactor = Math.min(photoW/targetW, photoH/targetH); 
     } 

     /* Set bitmap options to scale the image decode target */ 
     bmOptions.inJustDecodeBounds = false; 
     bmOptions.inSampleSize = scaleFactor; 
     bmOptions.inPurgeable = true; 

     /* Decode the JPEG file into a Bitmap */ 
     Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 

     /* Associate the Bitmap to the ImageView */ 
     mImageView.setImageBitmap(bitmap); 
     mImageView.setVisibility(View.VISIBLE); 
    } 

回答

1

我覺得納撒尼爾是給你很好的建議移動相機拍攝的意圖發射到的onResume這樣做。

但是,你需要一個的onResume那是你的活動開始從一個第一次,這是因爲你的活動相機意圖完成後恢復的發生之間進行區分。如果你不這樣做,你會看到你看到的循環。

要做到這一點,你可以改變你的onActivityResult()在你的Activity中設置一個成員變量,叫做isResumingFromCaptureIntent。當resultCode匹配您用來啓動相機意圖的內容時,將其設置爲onActivityResult中的true。然後,在你的onResume,檢查isResumingFromCaptureIntent,如果是真的,你知道你不需要啓動相機意圖,並可以設置是爲false,並與其他任何您的活動需要做繼續。

+0

那是不是的onCreate做和檢查,如果的onCreate被稱爲尚好? –

+1

是的,因爲的onCreate不能保證您拍攝活動回來時調用。當操作系統啓動攝像頭活動(或用戶導航到其他活動)時,操作系統可能會選擇保持活動狀態,並提供足夠的可用內存來保持活動的常駐狀態。 onResume將始終被調用。 – mmeyer

+0

我會做出改變,謝謝你的解釋! –

1

看吧:

Android: onCreate() getting called multiple times (and not by me)

一個指導的一塊,我可以提供是嘗試並移動呼叫

public void onResume(){ 

} 

你會得到自動的期望行爲去相機,但這可能會減少一些額外的電話,因爲它只發生在實際向用戶顯示活動時(包括返回fr其他應用程序等)。

+0

我試過把它放在onStart之前,我肯定會給這個試試謝謝 –

+0

不是問題,讓我知道它是否有效,我會進一步看。 –

+0

它沒有工作。在onResume方法中啓動相機意圖創建一個循環。一旦相機意圖完成它重新啓動 –

0

在onCreate方法我添加邏輯以檢查是否的onCreate此前被調用。我通過檢查傳入的包是空或不是

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mImageView = (ImageView) findViewById(R.id.imageView1); 
     mImageBitmap = null; 

     Button picBtn = (Button) findViewById(R.id.pictureButton); 
     setBtnListenerOrDisable(
       picBtn, 
       mTakePicOnClickListener, 
       MediaStore.ACTION_IMAGE_CAPTURE 
     ); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { 
      mAlbumStorageDirFactory = new FroyoAlbumDirFactory(); 
     } else { 
      mAlbumStorageDirFactory = new BaseAlbumDirFactory(); 
     } 

     if(savedInstanceState == null) 
      dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B); 
    }