1

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

我在我的活动中的 onResume 方法中创建了一个对象相机,这可以很好地显示预览显示,但是当我的活动暂停时,表面视图被破坏,我必须释放相机对象,然后如果我的活动进入 onresume android throws我的相机对象中有一个 nullPointerException。

我不确定为什么会发生这种情况,我将注释用于调试每个方法并查看会发生什么,显然一切都很好,只有在相机对象变为 null 的 surfaceCreated 方法中。

这是我的课:

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();
}

}

4

3 回答 3

2

我遇到了同样的问题。我解决它的方法是在我释放相机的同时(在 onPause() 方法上)在 SurfaceHolder 中调用 removeCallbak() 方法。我想在你的例子中是

holder.removeCallback(this);

这个想法是,不仅您在活动的 onPause() 方法上释放相机,而且还删除了 SurfaceHolder 上设置的回调。如果您不这样做,则会第二次出现错误,因为回调是针对您关联的第一个实例而不是第二个实例进行的。如果你也在 onPause() 上删除回调,你应该不会有任何问题。

于 2013-06-01T23:27:38.220 回答
0

我有这样的问题,在 AndroidManifest.xml 中添加这 3 行后,我的应用程序工作了

<uses-permission android:name="android.permission.CAMERA" />    
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

也许它会帮助你:)

于 2012-10-17T13:06:19.697 回答
0
public void release() {
    camera.release();
    camera = null;
}

这应该是:

public void release() {
   if(camera!=null) camera.release();
    camera = null;
}
于 2015-11-09T05:57:13.737 回答