我正在制作一个使用相机的应用程序,我希望在应用程序打开后立即打开默认相机。我目前在主要活动的onCreate方法中启动我的图像捕获意图。这有时非常精细,但有时相机意图连续3次启动。
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);
}
答案 0 :(得分:1)
看这里:
Android: onCreate() getting called multiple times (and not by me)
我可以提供的一条指导是尝试将该呼叫转移到
public void onResume(){
}
您将获得自动进入摄像机的所需行为,但这可能会减少一些额外的呼叫,因为它只会在活动实际显示给用户时发生(包括从其他人返回)应用程序等..)。
答案 1 :(得分:1)
我认为纳撒尼尔正在给你很好的建议,将你的相机意图推向onResume。
但是,您需要区分一个onResume,它是您第一次启动的活动,因为您的活动在相机意图完成后恢复活动。如果你不这样做,你就会得到你看到的循环。
为此,您可以更改onActivityResult()以在Activity中设置一个名为isResumingFromCaptureIntent的成员变量。当resultCode与用于启动摄像头意图的内容匹配时,在onActivityResult中将其设置为true。然后,在你的onResume中,检查isResumingFromCaptureIntent,如果为true,你知道你不需要启动相机意图并且可以设置为false并继续你的活动需要做的任何事情。
答案 2 :(得分: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);
}