当surfaceCreated方法第二次执行时,相机为null [英] Camera is null when the surfaceCreated method is executed for second time

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

问题描述

当第二次调用SurfaceHolder.Callback的surfaceCreated方法时,我对camera对象有问题,我的意思是:

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

我创建一个对象摄像头我的onResume方法从我的活动,这工作精细显示预览显示,但是当我的活动去暂停surfaceview被销毁,我必须释放相机对象,然后如果我的活动到onresume android在我的相机中抛出一个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方法相机对象变为null。

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.

这是我的类:

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()方法)调用我的SurfaceHolder中的removeCallbak()方法。我想在你的例子中是

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方法第二次执行时,相机为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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