Android 相机开发 报Camera is being used after Camera.release() was called错误

1、错误如图所示:


image.png

这个问题是关于相机调用后的释放问题
解决如下:
在activity的 onPause、onDestroy中加入如下代码

 @Override
    protected void onPause() {
        super.onPause();
        releaseCamera();              // release the camera immediately on pause event
    }

    private void releaseCamera() {
        if (mCamera != null){
            mCamera.setPreviewCallback(null);
            mPreview.getHolder().removeCallback(mPreview);
            mCamera.stopPreview();
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseCameraAndPreview();
    }

    private void releaseCameraAndPreview() {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
        if(mPreview != null){
            mPreview.destroyDrawingCache();
        }
    }

你可能感兴趣的:(Android 相机开发 报Camera is being used after Camera.release() was called错误)