31

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

Community
  • 1
  • 1
teepusink
  • 27,444
  • 37
  • 107
  • 147

9 Answers9

28

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;
        }
    }
}
Kangur
  • 7,823
  • 3
  • 30
  • 32
  • The error is very cryptic, and I encounter this issue even between just starting and stopping previews (there is engine level coding that is messing up royally and its not your fault). I find that releasing the camera between starting and stopping previews fixes this error – Aggressor Jul 17 '15 at 20:43
17

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    I still get this error randomly. One of my activities connect to camera, take some picture on emulator, passes on the data to next activity. When I restart the activity with some delay, the application crashes with the same error. But this works next time when i launch the complete application as fresh – Nayn Jul 26 '10 at 06:27
  • Right, the permission should be outside of tag – vir us Nov 10 '14 at 13:14
5

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;
        }
plus-
  • 45,453
  • 15
  • 60
  • 73
4

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

Sharjeel
  • 15,588
  • 14
  • 58
  • 89
  • It would have to be an application that doesn't properly release the camera though, right? Are there any relatively common apps that don't release the camera? – Kat Apr 17 '14 at 01:08
2

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.

Gerard
  • 2,832
  • 3
  • 27
  • 39
PankajAndroid
  • 2,689
  • 3
  • 27
  • 41
  • 1
    @Override protected void onDestroy() { try { camera.stopPreview(); camera.setPreviewCallback(null); try{camera.release();}catch (Exception e) {} camera = null; preview = null; } catch (Exception e) {} super.onDestroy(); } – PankajAndroid Dec 25 '12 at 11:26
2

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

wizurd
  • 3,541
  • 3
  • 33
  • 50
1

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

matt burns
  • 24,742
  • 13
  • 105
  • 107
0

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.

birdman
  • 1,134
  • 13
  • 13
0

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.

Brian
  • 147
  • 1
  • 4