android java lang runtimeexception 无法连接到相机服务 [英] android java lang runtimeexception fail to connect to camera service

查看:52
本文介绍了android java lang runtimeexception 无法连接到相机服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究手电筒的开/关.我收到此错误 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.

我的代码主代码.

我的清单权限:

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

推荐答案

试试这个...

 static Camera camera = null;

在顶部声明它.

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

这篇关于android java lang runtimeexception 无法连接到相机服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆