简体   繁体   English

相机-> ActivityForResult-> BackPress->相机

[英]Camera -> ActivityForResult -> BackPress -> Camera

I'm currently using a custom camera, however when I choose to select a image from the gallery and I press back and then continue to take a picture, it crashes on a nullpointer in the mCamera instance. 我当前使用的是自定义相机,但是当我选择从图库中选择图像并按回去并继续拍照时,它会在mCamera实例中的空指针上崩溃。

When I open the gallery I choose to release the camera, this works. 当我打开图库时,我选择释放相机,这可以工作。 After that when I press back, it comes in the activity for result. 之后,当我按回去时,它将进入结果活动。 From there I manually call the method that creates all the instances the camera needs. 从那里,我手动调用创建相机需要的所有实例的方法。 However when calling the mCamera.open it gives an error, which I catch. 但是,当调用mCamera.open时,它给出了一个错误,我抓住了。 But when I want to take a picture it crashes because the mCamera is a null. 但是,当我要拍照时,它会崩溃,因为mCamera为空。

What am I doing wrong here? 我在这里做错了什么? I release the camera so it should be able to get a new one. 我释放了相机,因此应该可以购买新相机。

Edit: 编辑:

Release: 发布:

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, 100);

        try {
            mCamera.release();
            Log.e("Camera", "Camera has been released");
        } catch (NullPointerException E){
            E.printStackTrace();
        }

Create: 创造:

// Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);

Correct solution: 正确的解决方案:

http://www.stackoverflow.com/a/19312951/867591 http://www.stackoverflow.com/a/19312951/867591

Thank you Ahmed! 谢谢艾哈迈德!

"It is right, you have open your camera in the onStart method and release it in the onStop method of your activity. The methods onResume and onPause are part of the visible lifecycle of the android activity. OnStop is called when another activity occupies the whole visible space. OnPause is even called when another activity comes to the foreground even if it does not occupy the whole visible space such as the intent chooser dialog does when it pops up. So I guess moving your camera creation and release into the correct lifecycle methods should do the trick. You can find further information about the activity lifecycle here, but I'm sure you are familiar with that: “是的,您已经在活动的onStart方法中打开了相机,并在活动的onStop方法中将其释放。方法onResume和onPause是android活动可见生命周期的一部分。当另一个活动占据整个活动时,就会调用OnStop可见空间,甚至当另一个活动不占据整个可见空间时,OnPause也会被调用,即使它没有像弹出时的意图选择器对话框那样占据整个可见空间,所以我想将您的摄影机创建转移到正确的生命周期方法中应该可以解决问题的方法。您可以在此处找到有关活动生命周期的更多信息,但是我敢肯定您对此很熟悉:

http://developer.android.com/reference/android/app/Activity.html " http://developer.android.com/reference/android/app/Activity.html

What did I do wrong? 我做错了什么? I actually did all of my creating stuff in the onResume, however switching it to the onStart fixed the issue. 实际上,我在onResume中完成了所有创建工作,但是将其切换为onStart可以解决此问题。

make sure that you're calling the getCameraInstance() method in onCreate() method,because if you're calling it outside the onCreate() method the camera will not be enabled and will give ua null pointer exception. 请确保你调用getCameraInstance()方法中onCreate()方法,因为如果你调用它外面onCreate()方法的相机不会被启用,并给UA空指针异常。

I think this link can help you out: 我认为此链接可以帮助您:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM