当surfaceCreated方法是第二次执行的摄像头为空 [英] Camera is null when the surfaceCreated method is executed for second time

查看:179
本文介绍了当surfaceCreated方法是第二次执行的摄像头为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相机对象的问题时,呼吁第二次一SurfaceHolder.Callback的surfaceCreated方法,我的意思是:

I have a problem with the camera object when the surfaceCreated method of a SurfaceHolder.Callback is called for second time, I mean:

我创造我从我的活动onResume方法的对象,摄像头,并且这工作正常显示preVIEW显示,但是当我的活动进入暂停surfaceview被破坏,我不得不释放相机对象,然后,如果我的活动涉及到onresume的Andr​​oid引发了我一个NullPointerException异常在我的相机对象。

I create an object camera in my onResume method from my activity, and this works fine showing the preview display, but when my activity goes to pause the surfaceview is destroyed and I have to release the camera object then if my activity comes to onresume android throws me a nullPointerException in my camera object.

我不知道为什么发生这一点,我的推杆评论调试每个方法,看看会发生什么,显然一切都很好仅在surfaceCreated方法相机对象成为空。

I'm not sure why is happening this, I putted comments to debug every method and see what happens, apparently everything is fine only in the surfaceCreated method the camera object becomes to null.

这是我的类:

公共类CameraRecord实现SurfaceHolder.Callback {

public class CameraRecord implements SurfaceHolder.Callback{

public static final int BACK_CAMERA = 1;
public static final int FRONT_CAMERA = 2;
//private SurfaceView surface;
private SurfaceHolder holder;
private Camera camera;

public CameraRecord(SurfaceView surface){
//  this.surface = surface;
    holder = surface.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    Log.e("CameraRecord","constructor");
}


public void openCamera(int wichCamera) throws Exception {

    if (wichCamera == BACK_CAMERA)
        camera = Camera.open();
    else {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int cameraCount = Camera.getNumberOfCameras();

        for (int i = 0; i < cameraCount; i++ ) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
                try {
                    camera = Camera.open(i); 
                    Log.e("CameraRecord","camera is CAMERA_FACING_FRONT " + cameraInfo.toString());
                } catch (RuntimeException e) {
                    e.printStackTrace();
                }
        }
    }

    if (camera == null)
        Log.e("CameraRecord","openCamera camera is null");
    else
        Log.e("CameraRecord","openCamera camera is not null");

}

public void start() throws IOException {
    camera.startPreview();
}

public void stop() {
    camera.stopPreview();
    Log.e("CameraRecord","stop camera");
}

public void release() {
    camera.release();
    camera = null;
}




@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    Log.e("CameraRecord", "surfaceChanged");
    try {
        start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
//      if (holder == null)
//          Log.e("CameraRecord","holder is null");
//      else
//          Log.e("CameraRecord","holder is not null");

    try {
        if (camera == null)
            Log.e("CameraRecord","camera is null");
        else
            Log.e("CameraRecord","camera is not null");

        camera.setPreviewDisplay(holder);
//          start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.e("CameraRecord", "surfaceDestroyed");
    stop();
    release();
}

}

推荐答案

我有同样的问题。我解决它在我被释放的相机(上的onPause()方法),同时调用removeCallbak()方法在我SurfaceHolder的方式。我想,在你的例子是

I was having this same problem. The way that I solved it was calling removeCallbak() method in my SurfaceHolder at the same time that I was releasing the camera (on the onPause() method). I guess that in your example would be

holder.removeCallback(this);

我们的想法是,不仅你释放相机的活动的onPause()方法,同时也删除回调的SurfaceHolder设置。如果你不这样做会出现错误,第二次是因为回调正在给你联系的第一个实例,而不是第二个。如果您删除的回调也对在onPause(),你应该没有任何问题。

The idea is that not only you release the camera on the onPause() method of your activity, but also you remove the callback set on the SurfaceHolder. If you don't do this the error would appear the second time because the callbacks are being made to the first instance that you associated and not the second one. If you remove the callback also on onPause(), you shouldn't have any problems.

这篇关于当surfaceCreated方法是第二次执行的摄像头为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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