How to fix “Fail to connect to camera service” exception in Android emulator

前端 未结 8 1691
情书的邮戳
情书的邮戳 2020-12-01 06:35

I\'m getting a Fail to connect to camera service exception when I run my Android app in the emulator. I\'ve read the various existing posts but none have fixed this. It is

8条回答
  •  执念已碎
    2020-12-01 06:57

    From the Android Developers Docs:

    Calling Camera.open() throws an exception if the camera is already in use by another application, so we wrap it in a try block.

    Try wrapping that code in a try catch block like so:

    try {
        releaseCameraAndPreview();
        if (camId == 0) {
            mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
        }
        else {
            mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        }
    } catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }
    

    Then add this function somewhere:

    private void releaseCameraAndPreview() {
        myCameraPreview.setCamera(null);
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }
    

提交回复
热议问题