Jigar Shekh Jigar Shekh - 6 months ago 634
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

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);
}