10

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136
gethan
  • 103
  • 1
  • 1
  • 6
  • 5
    Despite the formating issues with the manifest part I'd say that you have to use tags instead of ones for the flashlight and the camera. Also to make this clear, please post the correctly formated manifest part (and probably the whole stacktrace, if this is a permission issue, there should be a hint before the exception in the logcat). –  Jul 31 '11 at 01:45

4 Answers4

35

To access the device camera, you must declare the CAMERA permission in your Android Manifest like this,

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
Michael Wang
  • 9,583
  • 1
  • 19
  • 17
  • 10
    thats not totally true, you don't need the uses-feature tag for accessing the camera: "Declared elements are informational only, meaning that the Android system itself does not check for matching feature support on the device before installing an application. However, other services (such as Google Play) or applications may check your application's declarations as part of handling or interacting with your application. " http://developer.android.com/guide/topics/manifest/uses-feature-element.html – stoefln Jul 07 '12 at 08:49
9

You might have forgotten to call release in onDestroy

For example:

@Override
protected void onDestroy() {
    if (mCamera != null) {
        mCamera.release();
    }
    super.onDestroy();
}
eebbesen
  • 5,070
  • 8
  • 48
  • 70
bitsabhi
  • 728
  • 8
  • 12
3

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

Marko
  • 20,385
  • 13
  • 48
  • 64
rsc
  • 10,348
  • 5
  • 39
  • 36
3

You need to add the new request permission on android 6.x programmatically before.

 private static final int MY_PERMISSIONS_REQUEST_CAMERA = 555;

if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
        } else {
            IntentIntegrator.forSupportFragment(this).initiateScan();
        }

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CAMERA: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                IntentIntegrator.forSupportFragment(this).initiateScan();
            } else {
                FragmentHelper.popFragment(getActivity(), null, null);
            }
        }
    }
}
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
lucasddaniel
  • 1,779
  • 22
  • 22