Jigar Shekh Jigar Shekh - 1 year ago 932
Android Question

android java lang runtimeexception fail to connect to camera service

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"/>

Answer Source

try this...

 static Camera camera = null;

declare it on top.

   if(clickOn == true) {
       clickOn = false;
       camera = Camera.open();
       Parameters parameters = camera.getParameters();

       remoteViews.setViewVisibility(R.id.button1, View.GONE);
       remoteViews.setViewVisibility(R.id.button2, View.VISIBLE);
       localAppWidgetManager.updateAppWidget(componentName, remoteViews);
   } else {
       clickOn = true;
       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);