android java lang runtimeexception 无法连接到相机服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23904459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android java lang runtimeexception fail to connect to camera service
提问by Jigar Shekh
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.
我目前正在研究手电筒开/关。我收到此错误java.lang.RuntimeException: Fail to connect to camera service
我不知道为什么会发生此错误。我参考了很多解决方案,但我的问题仍然没有解决。当手电筒打开时,不会发生错误,但当手电筒关闭时,会发生错误。
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"/>
采纳答案by Jay Vyas
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);
}
回答by sam
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
我遇到了同样的问题,这里的答案都没有解决,所以在解决它之后,我添加了我的解决方法。这适用于支持按应用设置权限的新 android 版本(自 Marshmallow,6.0 起)。相机的权限可以被禁用,应该从应用程序设置中启用。设置 -> 应用 -> [你的应用] -> 权限
More info about this here: http://developer.android.com/training/permissions/requesting.html
更多关于这里的信息:http: //developer.android.com/training/permissions/requesting.html
回答by Jim Andreas
I also saw this error:
我也看到了这个错误:
java.lang.RuntimeException: Fail to connect to camera service
java.lang.RuntimeException:无法连接到相机服务
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:
在尝试手电筒应用程序时。事实证明,我对我的权限有点草率,并将它们复制到 manifest.xml 文件中的应用程序块的主体中。所以你真的需要遵守以下记录的语法:
http://developer.android.com/guide/topics/manifest/manifest-element.html
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:
否则,应用程序将因 Camera.open() 调用上的服务连接失败而失败。根据您在问题中的权限,它应该如下所示:
<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!
确保您的权限和功能列表仅包含在清单部分中,而不是隐藏在应用程序部分中!
回答by 0xAliHn
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.
如果您没有为您的应用启用相机权限,则在 android 6.0 中可能会出现此问题。从 Android 6.0 开始,您可以处理应用程序权限天气,您将为应用程序授予或不授予特定权限。
So, you need to enable permission from settings->apps->your_app->enable camera permission
if its not already enabled.
因此,settings->apps->your_app->enable camera permission
如果尚未启用,您需要启用权限。
回答by Mohammad nabil
If your os version is 6.0 or later version try this, hope this will help.
如果您的操作系统版本是 6.0 或更高版本,请尝试此操作,希望对您有所帮助。
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();
回答by Karim Baidar
if you try to open the camera using a cameraID that does not exist, you will receive that same error (java.lang.RuntimeException: Fail to Connect to camera service)
如果您尝试使用不存在的相机 ID 打开相机,您将收到同样的错误 (java.lang.RuntimeException: Fail to Connect to camera service)
look at your code at this block
在此块中查看您的代码
camera.setParameters(parameters);
camera.stopPreview();
camera.release();
camera = null;
Call release()
to release the camera for use by other applications. Applications should release the camera immediately in onPause()
(and re-open() it in onResume().
调用release()
以释放相机以供其他应用程序使用。应用程序应立即释放相机onPause()
(并重新打开()onResume().
In above code imediately after release you are putting null to camera
在发布后立即在上面的代码中,您将空值放入相机
回答by Bharadwaja Bapatla
Hi i hope you are dealing with a torch kind of app or something related to flash and there were many discussions went on this before and here are some useful links and tutorials to achieve your need, please go through them hope they may help you
嗨,我希望您正在处理一种手电筒类的应用程序或与 Flash 相关的东西,之前对此进行了很多讨论,这里有一些有用的链接和教程可以满足您的需求,请浏览它们,希望它们可以帮助您
How to turn on camera flash light programmatically in Android?
http://www.androidhive.info/2013/04/android-developing-flashlight-application/
http://www.androidhive.info/2013/04/android-developing-flashlight-application/
http://www.compiletimeerror.com/2013/08/how-to-turn-onoff-camera-led-flashlight.html#.U4WH5Xbc3o4
http://www.compiletimeerror.com/2013/08/how-to-turn-onoff-camera-led-flashlight.html#.U4WH5Xbc3o4
http://android.programmerguru.com/android-flashlight-example/
http://android.programmerguru.com/android-flashlight-example/
回答by anand krish
You need to stopPreview()
and release()
once you came back from camera,
so that other application can able to access it. Make the "Camera" class as static and refer it as null
in onPause()
. This resolves my Issue.
您需要stopPreview()
并且release()
一旦您从相机返回,以便其他应用程序能够访问它。使“相机”类静态和参考它null
在onPause()
。这解决了我的问题。
Try it out:
试试看:
public class CameraPhotoCapture extends Activity{
static Camera mcamera = null;
@Override
protected void onPause() {
// TODO Auto-generated method stub
if (mcamera != null) {
mcamera.stopPreview();
mcamera.release();
mcamera = null;
Log.d(DEBUG_TAG, "releaseCamera -- done");
}
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
if (mcamera != null) {
Camera.open();
Log.d(DEBUG_TAG, "openCamera -- done");
}
super.onResume();
}
}
回答by Keval Patel
The simple answer I can find to this problem is I was not asking for camera permission to the user, and that's why by default camera permission was not available to my app on Marshmallow devices. I simply added permission check for the camera before starting the camera and everything works fine.
对于这个问题,我可以找到的简单答案是我没有向用户请求相机权限,这就是为什么默认情况下,我的应用程序在 Marshmallow 设备上无法获得相机权限。我只是在启动相机之前添加了相机的权限检查,一切正常。
回答by Saint
private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
}
Check the camera permission at runtime, and request the permission if it has not be granted. It works for me.
在运行时检查摄像头权限,如果没有授予权限,则请求权限。这个对我有用。
if (checkPermission()) {
initCamera();
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}