Home » Android » Android – Fail to connect to camera

Android – Fail to connect to camera

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using the Android APIDemo sample code.

When I run the CameraPreview example, at first it was giving me an error.

I traced that one down and the sample was working for a while.
Now, it no longer works. It says

ERROR/AndroidRuntime(2949): java.lang.RuntimeException: Fail to connect to camera service  

What can be causing that? It happens when camera.open() is called.

Thanks,
Tee

How to&Answers:

Be sure to properly release all the aquired camera resources:

    @Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (mCam != null) {
        mCam.stopPreview();
        mCam.setPreviewCallback(null);
        mCam.release();
        mCam = null;
    }
}

    @Override
public void surfaceCreated(SurfaceHolder holder) {
    if (mCam == null) {
        mCam = Camera.open();
        try {
            mCam.setPreviewDisplay(holder);

            // TODO test how much setPreviewCallbackWithBuffer is faster
            mCam.setPreviewCallback(this);
        } catch (IOException e) {
            mCam.release();
            mCam = null;
        }
    }
}

Answer:

Make sure your <uses-permission> elements are in the proper positions in your AndroidManifest.xml file.

Answer:

It happens if your activity does not close the camera properly in surfaceDestroyed or onConfigurationChanged etc…

Don’t forget to do this everytime you go out of your activity:

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

Answer:

Another reason of this error is when you try to open camera but some other application or even your application is already using camera.

Answer:

I also get this type of issue on a HTC device. To solve add this code:

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (camera!=null)
    {
        camera.stopPreview();
        camera.release();
        camera=null;
    }
}

And yet you cannot start camera then restart device.

Answer:

Also, if you are using the emulator, make sure you have selected to Emulate the Front Camera and/or the Back Camera.

Android Virtual Device Manager->Select Device->Edit->Front Camera->Emulated

Answer:

As others mention, you have to call release() on your camera object when you’re finished.

I wasn’t doing this initially, so I changed my code but it still gave me the same error. I was deploying directly to a physical handset and had to restart the phone before it worked

Answer:

I also received this error when I was testing and stopped execution before reaching the point in code when the:

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

was called. This then blocked the camera because it hadn’t een released properly. My solution was to turn the camera off and back on again. You can confirm this is the case by trying to use the inbuilt Camera app in your phone. It won’t work either because it is still busy.

Answer:

Second @matt-burns however you might want to check that you’re only trying to get the camera once. I had forgotten to comment out a line and was trying to launch two activities that would both try to obtain the camera.