teepusink
teepusink

Reputation: 28882

Android - Fail to connect to camera

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

Upvotes: 31

Views: 36369

Answers (9)

wizurd
wizurd

Reputation: 3729

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

Upvotes: 2

Brian
Brian

Reputation: 147

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.

Upvotes: 0

PankajAndroid
PankajAndroid

Reputation: 2707

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.

Upvotes: 2

birdman
birdman

Reputation: 1144

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.

Upvotes: 0

matt burns
matt burns

Reputation: 25370

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

Upvotes: 1

Sharjeel
Sharjeel

Reputation: 15798

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

Upvotes: 4

plus-
plus-

Reputation: 46543

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

Upvotes: 5

Krzysztof Madejski
Krzysztof Madejski

Reputation: 7978

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

Upvotes: 28

CommonsWare
CommonsWare

Reputation: 1006539

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

Upvotes: 17

Related Questions