gpt4 book ai didi

android - 第二次执行surfaceCreated方法时Camera为null

转载 作者:行者123 更新时间:2023-11-29 21:59:00 26 4
gpt4 key购买 nike

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

我在我的 Activity 的 onResume 方法中创建了一个对象相机,这在显示预览显示时效果很好,但是当我的 Activity 暂停时,表面 View 被破坏,如果我的 Activity 开始,我必须释放相机对象onresume android 在我的相机对象中抛出一个 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();
}

最佳答案

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

holder.removeCallback(this);

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

关于android - 第二次执行surfaceCreated方法时Camera为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12411346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com