android java lang runtimeexception fail to connect to camera service

99,885

Solution 1

try this...

 static Camera camera = null;

declare it on top.

 try{ 
   if(clickOn == true) {
       clickOn = false;
       camera = Camera.open();
       Parameters parameters = camera.getParameters();
       parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
       camera.setParameters(parameters);
       camera.startPreview();

       remoteViews.setViewVisibility(R.id.button1, View.GONE);
       remoteViews.setViewVisibility(R.id.button2, View.VISIBLE);
       localAppWidgetManager.updateAppWidget(componentName, remoteViews);
   } else {
       clickOn = true;
       camera.stopPreview();
       camera.release();
       camera = null;

       remoteViews.setViewVisibility(R.id.button1, View.VISIBLE);
       remoteViews.setViewVisibility(R.id.button2, View.GONE);
       localAppWidgetManager.updateAppWidget(componentName, remoteViews);
   }    
} catch(Exception e) {
   Log.e("Error", ""+e);
}

Solution 2

I had the same issue that none of the answers here solved, so after solving it I am adding my way of solving it. This applies to new 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. Settings -> Apps -> [Your App] -> Permissions

More info about this here: http://developer.android.com/training/permissions/requesting.html

Solution 3

I also saw this error:

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

while experimenting with a flashlight app. Turns out that I was a bit sloppy with my permissions and copied them into the body of the application block in the manifest.xml file. So you REALLY need to obey the syntax as documented in:

http://developer.android.com/guide/topics/manifest/manifest-element.html

Otherwise the app will fail with service connection failure on the Camera.open() call. It should look like this based on your permissions in the question:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>

<application

Make sure your permission and feature list is contained only in the manifest section, and not buried in the application section!

Solution 4

This problem may arise in android 6.0 if you didn't enable camera permission for your app. As from Android 6.0 you can handle the app permission weather you will give or not specific permission for an application.

So, you need to enable permission from settings->apps->your_app->enable camera permission if its not already enabled.

Solution 5

If your os version is 6.0 or later version try this, hope this will help.

public class RequestUserPermission {

private Activity activity;
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CAMERA
};

public RequestUserPermission(Activity activity) {
    this.activity = activity;
}

public  void verifyStoragePermissions() {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}
}


**//CALL FROM YOUR ACTIVITY**
 RequestUserPermission requestUserPermission = new RequestUserPermission(this);
    requestUserPermission.verifyStoragePermissions();
Share:
99,885
Jigar Shekh
Author by

Jigar Shekh

Updated on August 21, 2022

Comments

  • Jigar Shekh
    Jigar Shekh almost 2 years

    I am currently working on Flashlight On/OFF. I am getting this error java.lang.RuntimeException: Fail to connect to camera service I don't know why this error is occurring. I referred to many solutions but my problem was still not solved. When flashlight is on, the error does not occur but when the flashlight is off then the error occurs.

    My Code Main Code.

    My Manifest permission:

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus"/>