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); 
    }