无法打开相机连接相机时发生错误:0

无法打开相机连接相机时发生错误:0

问题描述:

我开始开发应用程序,需要使用手机的摄像头,当我使用Camera.open()方法(无论是否带有cameraId)时,都会返回错误连接相机时发生错误: 0。我的AndroidManifest.xml是:

I started developping an app and I need to use the camera of my phone, and when I use the method Camera.open(), either with cameraId or not, it returns the error "An error occurred while connecting to camera: 0". My AndroidManifest.xml is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.telecombretagne.holowater">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".camera"
            android:label="@string/title_activity_camera"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

我手机的Android版本是6.0.1,它是BQ Aquaris M5。

My phone's Android Version is 6.0.1, and it's a BQ Aquaris M5.

事先感谢。

运行棉花糖的设备需要在以下设备上设置权限运行时,这是我对另一个类似问题的回答 :)

Devices that are running Marshmallow requires permission to be set on runtime, here's my answer from another similar question here :)


来自 https://developer.android.com/training/permissions/requesting.html

注意:从Android 6.0(API级别23)开始,用户可以随时从任何应用撤消权限,即使该应用的目标是较低的API级别。无论您的应用定位到哪个API级别,都应测试您的应用以验证其在缺少所需权限时是否能够正常运行。

From https://developer.android.com/training/permissions/requesting.html
Note: Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level. You should test your app to verify that it behaves properly when it's missing a needed permission, regardless of what API level your app targets.

除了清单中设置的权限外,您还需要在运行时请求/检查权限。

Aside from the permissions set in the manifest, you'll need to request/check for permission on runtime. There are sample codes in there you can use, or...



快速解决方案,


Quick solution,


转到设置->应用程序->(您的应用程序名称)->权限,然后启用相机权限。
完成,尽管不建议用于最终产品

go to Settings-> Apps->(Your app name)->Permissions and enable the camera permission. Done, although not recommended for final product

,然后重试您的应用。应该现在可以工作了:D

then try your app again. Should be working now :D