摄像头活动返回null机器人机器人、摄像头、null

由网友(独臂大虾)分享简介:我建立我想要捕获的默认摄像头的活动图像,并返回到我的活动,并加载图像中的ImageView的应用程序。现在的问题是摄像头的活动总是返回null。在我的onActivityResult(INT申请code,INT结果code,意图数据)的方法,我得到的数据为空。这是我的code I am building an app...

我建立我想要捕获的默认摄像头的活动图像,并返回到我的活动,并加载图像中的ImageView的应用程序。现在的问题是摄像头的活动总是返回null。在我的onActivityResult(INT申请code,INT结果code,意图数据)的方法,我得到的数据为空。这是我的code

I am building an application where i want to capture an image by the default camera activity and return back to my activity and load that image in a imageview. The problem is camera activity always returning null. In my onActivityResult(int requestCode, int resultCode, Intent data) method i am getting data as null. Here is my code

public class CameraCapture extends Activity {

protected boolean _taken = true;
File sdImageMainDirectory;
Uri outputFileUri;

protected static final String PHOTO_TAKEN = "photo_taken";
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;

@Override
public void onCreate(Bundle savedInstanceState) {

    try {

        super.onCreate(savedInstanceState);   
        setContentView(R.layout.cameracapturedimage);
                File root = new File(Environment
                        .getExternalStorageDirectory()
                        + File.separator + "myDir" + File.separator);
                root.mkdirs();
                sdImageMainDirectory = new File(root, "myPicName");



            startCameraActivity();

    } catch (Exception e) {
        finish();
        Toast.makeText(this, "Error occured. Please try again later.",
                Toast.LENGTH_SHORT).show();
    }

}

protected void startCameraActivity() {

    outputFileUri = Uri.fromFile(sdImageMainDirectory);

    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

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

    switch (requestCode) {
    case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
    {
        if(resultCode==Activity.RESULT_OK)
        {
            try{
            ImageView imageView=(ImageView)findViewById(R.id.cameraImage);
            imageView.setImageBitmap((Bitmap) data.getExtras().get("data"));
            }
            catch (Exception e) {
                // TODO: handle exception
            }
        }

        break;
    }

    default:
        break;
    }
}

 @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {
        _taken = true;
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);
}

我在做什么错??????????

Am i doing anything wrong??????????

推荐答案

你得到错误的,因为你正在做错误的方式。

You are getting wrong because you are doing it wrong way.

如果您通过了额外的参数 MediaStore.EXTRA_OUTPUT 用相机意图,然后相机活动将写入所拍摄的图像,以这条道路,它不会返回位图中的 onActivityResult 方法。

If you pass the extra parameter MediaStore.EXTRA_OUTPUT with the camera intent then camera activity will write the captured image to that path and it will not return the bitmap in the onActivityResult method.

如果您将检查你所传递则路径,你就会知道,其实相机有写在这条道路捕获的文件。

If you will check the path which you are passing then you will know that actually camera had write the captured file in that path.

有关更多信息,你可以按照this, 这和this

For further information you can follow this, this and this

阅读全文

相关推荐

最新文章