美文网首页
Android 相机开发 报Camera is being u

Android 相机开发 报Camera is being u

作者: 黑芝麻胡 | 来源:发表于2021-01-14 10:12 被阅读0次

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 u

      本文链接:https://www.haomeiwen.com/subject/ynsnoktx.html