Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.RuntimeException: Fail to Connect to camera service

Tags:

android

camera

I tried to make an app than can switch my camera flash on and off. The code I have atm looks like this:

Camera flash;
Camera.Parameters params;

flash = Camera.open();
params = flash.getParameters();

params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);        
flash.setParameters(params);

And in the manifest xml:

<permission android:name="android.permission.FLASHLIGHT"  

android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
          android:protectionLevel="normal" />

<permission android:name="android.permission.CAMERA"> </permission>

Everytime I run the code, the app crashes at "flash = Camera.open();" with this error:

java.lang.RuntimeException: Fail to Connect to camera service

What am I doing wrong?

like image 782
gethan Avatar asked Jul 31 '11 00:07

gethan


2 Answers

You might have forgotten to call release in onDestroy

For example:

@Override
protected void onDestroy() {
    if (mCamera != null) {
        mCamera.release();
    }
    super.onDestroy();
}
like image 151
bitsabhi Avatar answered Oct 11 '22 21:10

bitsabhi


Usually that problem is due to the missing camera request permission, as already said by other users.

But, just to register here another cause, if you try to open the camera using a cameraID that does not exist, you will receive that same error

java.lang.RuntimeException: Fail to Connect to camera service

like image 33
rsc Avatar answered Oct 11 '22 20:10

rsc