Failed to connect to camera service

I’m trying to access the camera on my phone. I’m writing a simple stub app prior to putting the code in a widget. I’m not getting very far. The code always throws a runtime exception “failed to connect to camera service” The code(pinched from a commonsware example) which goes wrong is:

    @Override
    public void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
                Camera.getCameraInfo(i, info);
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    try {
                        // Gets to here OK
                        camera = Camera.open(i);
                    } catch (Exception e) {
                        e.printStackTrace();
                        //  throws runtime exception :"Failed to connect to camera service"
                    }
                }
            }
        }
}

and my manifest is (corrected 20th Oct):

  • Google+ Login Not working properly on Android fragment
  • How to model game object rendering and behavior in modular way?
  • Android: Re-invoke application if task manager kill
  • Android launches system settings instead of my app
  • How to create an UI or a widget that sits on top of all applications in Android?
  • UnsatisfiedLinkError In Android 4.4 libDevil cannot locate symbol “png_set_longjmp_fn”
  •  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.nbt.cameratest"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk android:minSdkVersion="9" />
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:label="@string/app_name"
                android:name=".CameraTestActivity" >
                <intent-filter >
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
                <uses-permission android:name="android.permission.CAMERA" />
                <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        </application>
    </manifest>
    

    Can anyone please suggest what might be wrong?

    UPDATE 20th Oct

    Logcat in SDK 4.0 is broken and wont show the end of the log, so I’ve cut this bit as best as I can from command line adb logcat:

    W/ServiceManager( 2588): Permission failure: android.permission.CAMERA from uid=10136 pid=5744
    E/CameraService( 2588): Permission Denial: can't use the camera pid=5744, uid=10136
    W/System.err( 5744): java.lang.RuntimeException: Fail to connect to camera service
    W/System.err( 5744):    at android.hardware.Camera.native_setup(Native Method)
    W/System.err( 5744):    at android.hardware.Camera.<init>(Camera.java:294)
    W/System.err( 5744):    at android.hardware.Camera.open(Camera.java:255)
    etc..
    

    I don’t know why I haven’t permission as it is declared in the manifest

    Related posts:

    Using ProGuard with Android
    Simple swipe gesture to activity tutorial?
    Listen and respond to ANR?
    How to set text size of textview dynamically for different screens
    reading whatsapp messages
    Gradient styles
  • Foreground service example
  • Android JellyBean not Recognizing getTextSize of AttriubteSet
  • How can I start new activity as an expended circle from center
  • apply plugin: 'android' or apply plugin: 'com.android.application'
  • Replacing Fragments isn't working/Am I executing this the proper way?
  • Android: Activity Dialog / Dialog disappears unexpectedly
  • 7 Solutions collect form web for “Failed to connect to camera service”

    Few things:

    1. Why are your use-permissions and use-features tags in your activity tag. Generally, permissions are included as direct children of your <manifest> tag. This could be part of the problem.

    2. According to the android camera open documentation, a runtime exception is thrown:

      if connection to the camera service fails (for example, if the camera is in use by another process or device policy manager has disabled the camera)

      Have you tried checking if the camera is being used by something else or if your policy manager has some setting where the camera is turned off?

    3. Don’t forget the <uses-feature android:name="android.hardware.camera.autofocus" /> for autofocus.

    While I’m not sure if any of these will directly help you, I think they’re worth investigating if for no other reason than to simply rule out. Due diligence if you will.

    EDIT
    As mentioned in the comments below, the solution was to move the uses-permissions up to above the application tag.

    I had the same issue that none of the above solved, after solving it I am adding an answer that could solve the same issue but for newer android versions that support setting permissions per app (since Marshmallow, 6.0). The permission for camera could be disabled and should be enabled from the app settings.

    Make sure to call the release() method to release the camera when it is no longer needed, or you will not be able to use the camera. Perhaps as a sanity check, see if your regular camera works. If it says it fails, then your previous attempts at runni

    I know this question has been answered long time ago, but I would like to add a small thing.

    To everyone having the same error, make sure to add this permission in you manifest file:

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

    IMPORTANT to use the CAPITAL letters for CAMERA, as I had the permission with small letters and it didn’t work.

    The problem is related to permission. Copy following code into onCreate() method. The issue will get solved.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[] {Manifest.permission.CAMERA}, 1);
        }
    }
    

    After that wait for the user action and handle his decision.

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case CAMERA_PERMISSION_REQUEST_CODE:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //Start your camera handling here
                } else {
                    AppUtils.showUserMessage("You declined to allow the app to access your camera", this);
                }
        }
    }
    

    running your code hundred times may affect the camera to function wrongly.Your activity may be performing correctly but system could not buy it.so camera forces stop.
    One main tip all missed is rebooting your phone and not only eclipse..It worked for me..

    I had the same issue. Tried all the solutions mentioned here but nothing worked. So i rebooted my device and suddenly the issue solved.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.