java.lang.RuntimeException: Camera is being used after Camera.release() was called

 java.lang.RuntimeException: Camera is being used after Camera.release() was called

在视频录制的时候出现过这个异常.然而却没有在个人写的代码中显示出来


java.lang.RuntimeException: Camera is being used after Camera.release() was called_第1张图片


进一步观察,发现是点击切换摄像头的时候出现的异常:

 cameraSelection = ((cameraSelection == CameraInfo.CAMERA_FACING_BACK) ? CameraInfo.CAMERA_FACING_FRONT : CameraInfo.CAMERA_FACING_BACK);
                    initCameraLayout();

                    if (cameraSelection == CameraInfo.CAMERA_FACING_FRONT) {

                    }
                    //                flashIcon.setVisibility(View.GONE);
                    else {
                        //                flashIcon.setVisibility(View.VISIBLE);
                        if (isFlashOn) {
                            cameraParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
                            mCamera.setParameters(cameraParameters);
                        }
                    }

问题来了,因为去搜索这个异常,结果都是说我在Camera.release() 之前要执行 setPreviewCallback();

可是在我代码中,是不执行Camera.release() .

思考了很久,才想到,会不会是在切换摄像头的时候.Camera会自动release,然后再重新调用.

然后.在就点击切换摄像头之前.直接把camera释放掉.

/**
 * 释放摄像头资源
 */
private void freeCameraResource() {
    if (mCamera != null) {
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.lock();
        mCamera.release();
        mCamera = null;
    }
}


再执行,发现就好了.

你可能感兴趣的:(Android)