摄像头活动返回null机器人 [英] Camera activity returning null android

查看:139
本文介绍了摄像头活动返回null机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立我想要捕获的默认摄像头的活动图像,并返回到我的活动,并加载图像中的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.

有关更多信息,你可以按照<一个href="http://stackoverflow.com/questions/10042695/how-to-get-camera-result-as-a-uri-in-data-folder/10229228#10229228">this, 和<一href="http://dharmendra4android.blogspot.in/2012/04/save-captured-image-to-applications.html">this

For further information you can follow this, this and this

这篇关于摄像头活动返回null机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆