来自PreviewCallback的android相机发布错误

时间:2012-03-22 19:59:51

标签: android android-camera

所以我已经阅读了相机API,但在此找不到任何内容。我正在使用相机来抓取相框并且它完美地工作,直到我尝试释放相机。我在一个调用序列中复制了错误:

camera = Camera.open();
camera.setPreviewDisplay(getHolder());
Parameters params = camera.getParameters();
List<Size> ls = params.getSupportedPreviewSizes();
Size size = ls.get(1);
this.width = size.width;
this.height = size.height;
params.setPreviewSize(size.width, size.height);
camera.setParameters(params);
camera.setDisplayOrientation(90);
camera.setPreviewCallback(this);
camera.startPreview();
camera.stopPreview();
camera.release();

我得到的错误是

03-22 13:31:42.592: E/AndroidRuntime(14152): java.lang.RuntimeException: Method called after release()
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.hardware.Camera.setHasPreviewCallback(Native Method)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.hardware.Camera.access$600(Camera.java:114)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:545)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.os.Looper.loop(Looper.java:130)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at android.app.ActivityThread.main(ActivityThread.java:3684)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at java.lang.reflect.Method.invokeNative(Native Method)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at java.lang.reflect.Method.invoke(Method.java:507)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:845)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
03-22 13:31:42.592: E/AndroidRuntime(14152):    at dalvik.system.NativeStart.main(Native Method)

所以发生的事情不在于我的一个职能部门。 如果我注释掉camera.setPreviewCallback(this);然后这个错误没有出现,但我显然失去了我的回调,这是将相机包含在我的应用中的重点。

2 个答案:

答案 0 :(得分:64)

您需要在camera.release()之后camera.stopPreview()之前取消设置预览回调:

camera.setPreviewCallback(null);

否则可能会在相机释放后调用。

答案 1 :(得分:14)

作为旁注,完整的releaseCamera方法如下:

// release Camera for other applications
private void releaseCamera() {
    // check if Camera instance exists
    if (mCamera != null) {
        sPreviewing = false;
        // first stop preview
        mCamera.stopPreview();
        // then cancel its preview callback
        mCamera.setPreviewCallback(null);
        // and finally release it
        mCamera.release();
        // sanitize you Camera object holder
        mCamera = null;
    }
}