Android Camera将位图从Camera Intent传递到MainActivity

时间:2016-01-22 21:56:59

标签: android android-intent bitmap callback camera

我正在尝试拍摄照片并将其在我的MainActivity中设置为ImageView。 我希望将我的相机活动作为一个单独的类,所以我尝试了以下内容: MainActivity:

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.action_button:
            Intent camera = new Intent(MainActivity.this,Camera.class);
            startActivityForResult(camera ,CAMERA_IDENTIFITER);
            break;
        default:
            break;
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //super.onActivityResult(requestCode, resultCode, data);
    Log.d("onActivityResult","Sucessfully callbacked!"); //This one wont be executed!
    switch(requestCode) {
        case (CAMERA_IDENTIFITER) : {
            if (resultCode == Activity.RESULT_OK) {
                Bitmap bitmap = (Bitmap) data.getParcelableExtra(MediaStore.EXTRA_OUTPUT);
                mImageView.setImageBitmap(bitmap);
            }
            break;
        }
    }
}

这是我的相机活动:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);
    takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    pm = getPackageManager();
}
@Override
protected void onStart() {
    super.onStart();
    if(!isFromActivityResult){
        dispatchTakePictureIntent();
    }
}
public void dispatchTakePictureIntent() {
    if (takePictureIntent.resolveActivity(pm) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch {...}
        if (photoFile != null) {
            pictureTaken = true;
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    } 
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        try {
            bitmap = ImageFileHandler.handleSamplingAndRotationBitmap(this, Uri.fromFile(new File(mCurrentPhotoPath)));
            //mImageView.setImageBitmap(bitmap);
            resultIntent = new Intent();
            resultIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    bitmap);
            setResult(Activity.RESULT_OK, resultIntent);
            Log.d("onActivityResult","finish() executed!"); //This one gets executed!
            finish();
        } catch {...}
    }
}

相机意图正常启动但是当我拍照时我没有回到我的MainActivity中的onActivityResult,我被困在相机意图中。

1 个答案:

答案 0 :(得分:0)

执行onActivityResult()后会调用onStart()。因此一次又一次地启动相机意图。放入日志并测试你自己。 解决问题 在您的活动isFronActivityResult中保留一个布尔变量。将默认值保持为false并执行isFromActivityResult = true;在onActivityResult()中。在onStart中执行以下操作:

          @Override
        protected void onStart() {
            super.onStart();
            if(!isFromActivityResult)
              dispatchTakePictureIntent();
        }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
        try {
            bitmap = ImageFileHandler.handleSamplingAndRotationBitmap(this, Uri.fromFile(new File(mCurrentPhotoPath)));
            //mImageView.setImageBitmap(bitmap);
            resultIntent = new Intent();
            resultIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    bitmap);
            setResult(Activity.RESULT_OK, resultIntent);
            isFromActivityResult = true;// add this line
            finish();
        } catch {...}
    }
}