java Android 6.0 运行时异常:无法连接到相机服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33276517/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:27:58  来源:igfitidea点击:

Android 6.0 RuntimeException: Fail to connect to camera service

javaandroidandroid-cameraandroid-6.0-marshmallow

提问by HelloSilence

An error occurred with my app which ran in Nexus5 (Android6.0).

我的应用程序在 Nexus5 (Android6.0) 中运行时发生错误。

The error was

错误是

java.lang.RuntimeException: Fail to connect to camera service

java.lang.RuntimeException:无法连接到相机服务

I had added the permission to the AndroidManifest.xml.

我已将权限添加到 AndroidManifest.xml。

But the app is OK with another phone(Android5.1), and genymotion AVD(Android 4.0).

但是该应用程序可以与另一部手机(Android5.1)和genymotion AVD(Android 4.0)配合使用。

The key code is below

关键代码如下

 @Override
public void onResume() {
    super.onResume();
    try {
        mCamera = Camera.open();
    }catch (Exception e){
        Log.e(TAG,"open camera failed",e);
    }

}

回答by fahad_sust

Write in your gradle.build targetSdkVersion 22. It is a temporary solution but work.

在你的 gradle.build targetSdkVersion 22 中写入。这是一个临时解决方案,但有效。

回答by zzzmode

open Settings -> Apps -> click you app -> Permissions -> open Camera permissions.
you can see: http://developer.android.com/training/permissions/requesting.html

打开设置-> 应用程序-> 单击您的应用程序-> 权限-> 打开相机权限。
你可以看到:http: //developer.android.com/training/permissions/requesting.html

回答by Amalan Dhananjayan

This happens because, in Android 6.0 the permission model is different. You have to implement the new permission model which asks for the permission on run time. Even if you don't ask it, you can manually enable it in the phone's app setting, but that is not gona work when you publish your app in the play store. Hereis an Article on How to get Run time permissions

这是因为,在 Android 6.0 中,权限模型是不同的。您必须实现新的权限模型,该模型要求在运行时获得权限。即使您不要求,您也可以在手机的应用程序设置中手动启用它,但是当您在 Play 商店中发布您的应用程序时,这将不起作用。 是一篇关于如何获得运行时权限的文章

Also you can check Nick's answer herefor getting multiple permissions

您也可以在此处查看尼克的答案以获得多个权限

回答by 0xAliHn

From android 6.0 you can handle the app permission weather you will give or not specific permission for an application. In your case if you didn't enable camera permission for your app then this issues may arise.

从 android 6.0 开始,您可以处理应用程序权限天气,您将为应用程序授予或不授予特定权限。在您的情况下,如果您没有为您的应用启用相机权限,则可能会出现此问题。

So, you need to enable permission from settings->apps->your_app->enable camerapermission.

因此,您需要从settings->apps->your_app->enable camera许可中启用许可。

回答by Ramesh R

Add Camera Permission before Opening the Camera:

在打开相机之前添加相机权限:

follow the Link for adding Permissions: https://developer.android.com/training/permissions/requesting.html

按照链接添加权限:https: //developer.android.com/training/permissions/requesting.html