我正在使用相机闪光灯制作手电筒应用程序。它似乎工作正常,但有时调用camera.release()导致挂起大约一分钟左右。我已经包含了下面的代码。我看了一堆例子,我没有看到任何可能导致这种情况的事情。有什么想法吗?
//latest
public void setOn(boolean on, Context context) {
if (lock) {
Log.i(TAG, "lock: true");
return;
}
if (on) {
if (mCamera == null) {
mCamera = Camera.open();
}
Parameters params = mCamera.getParameters();
params.setFlashMode(MODE_TORCH);
mCamera.setParameters(params);
mCamera.startPreview();
} else {
if (mCamera != null) {
try {
Parameters params = mCamera.getParameters();
params.setFlashMode(MODE_OFF);
mCamera.setParameters(params);
} finally {
new Thread(new Runnable() {
public void run() {
Log.i(TAG, "new Thread - start");
lock = true;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
lock = false;
Log.i(TAG, "new Thread - end");
}
}).start();
}
}
}
}
//original
public void setOn(boolean on, Context context) {
Camera camera = mCamera;
if (on) {
if (camera == null) {
mCamera = camera = Camera.open();
}
Parameters params = camera.getParameters();
params.setFlashMode(MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
} else {
if (camera != null) {
try {
Parameters params = camera.getParameters();
params.setFlashMode(MODE_OFF);
camera.setParameters(params);
} finally {
camera.stopPreview();
camera.release();
mCamera = camera = null;
}
}
}
}
答案 0 :(得分:1)
尝试将它放在线程中以便在后台运行,这样它就不会挂断UI。
new Thread(new Runnable(){
public void run(){
camera.setPreviewCallback(null); // PreviewCallback de_init.
camera.stopPreview(); // stop Preview
camera.release();
}
}).start();
答案 1 :(得分:0)
对我来说,有效的解决方案是:
Try{
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
} catch (Exception e){
//...
}
答案 2 :(得分:0)
我已经解决了这个问题,在release()
之前添加了camera.unlock()camera.stopPreview();
camera.setPreviewCallback(null);
camera.unlock();
camera.release();
camera = null;
测试所需的更多设备...
答案 3 :(得分:0)
只需调用您的方法
即可mCamera.stopPreview();
mCamera.setPreviewCallback(null);
mCamera.release();
你必须在stopPreview和camera.releass之间调用setPreviewCallback(null)