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

大兔子大兔子 提交于 2019-11-27 07:26:11
aProperFox

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;
    }
}

With Android 6.0, this error can appened if you don't check for manifest authorisation :

    //If authorisation not granted for camera
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
        //ask for authorisation
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 50);
    else
        //start your camera
        this.startCamera();

OP mentions this in his question, but my issue was I forgot to enable camera emulation in my AVD emulator settings:

Using

if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  ActivityCompat.requestPermissions(CodeScanner.this, new String[]{android.Manifest.permission.CAMERA}, 50);
}

worked for me

1.Use below permissions in manifest file and always put permissions above Application tag.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature
    android:name="android.hardware.camera"
    android:required="false" />
<uses-feature android:name="android.hardware.camera.autofocus" />

2.Use Unlock while camera used by other service like MediaRecorder.

camera.unlock();
recorder.setCamera(camera);

2.Released camera properly, I prefer to use lock so become safely accessible for other application and second time use (In case if we reopen).

if (camera != null) {
        camera.lock();
        camera.stopPreview();
        camera.release();
        camera = null;
    }

If you periodically got a white screen instead your view of camera - use:

private void releaseCameraAndPreview() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCameraView.getHolder().removeCallback(mCameraView);
            mCamera.release();
            mCamera = null;
        }
    }

and put it here

try {
            releaseCameraAndPreview();
            mCamera = getCameraInstance();
        }...

and here

@Override
    protected void onPause() {
        super.onPause();
        releaseCameraAndPreview();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!