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

前端 未结 8 1642
情书的邮戳
情书的邮戳 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 07:04

    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;
        }
    
    0 讨论(0)
  • 2020-12-01 07:06

    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();
    
    0 讨论(0)
提交回复
热议问题