安卓的java郎的RuntimeException无法连接到摄像机服务连接到、摄像机、java、RuntimeException

2023-09-13 01:23:56 作者:被作业虐的人

我目前工作的手电筒开/关。我收到此错误了java.lang.RuntimeException:无法连接到摄像机服务我不知道为什么这个错误发生。我提到的很多解决方案,但仍然没有解决我的问题。当手电筒打开,不会发生错误,但是当手电筒是关闭的,然后发生错误。

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.

我的code 主要code 。

我的清单权限:

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

我该如何解决呢?请帮我。

How can I solve it? Please help me.

推荐答案

试试这个...

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