无法打开摄像头连接到摄像头时出错:0

时间:2016-06-17 20:20:44

标签: android camera

我开始开发应用程序,我需要使用手机的相机,当我使用Camera.open()方法时,无论是否使用cameraId,它都会返回错误“连接到相机时出错: 0" 。我的AndroidManifest.xml是:

<?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。

提前致谢。

2 个答案:

答案 0 :(得分:3)

运行Marshmallow的设备需要在运行时设置权限,这是我在另一个类似问题中的回答here :)

  

来自https://developer.android.com/training/permissions/requesting.html   
注意:从Android 6.0(API级别23)开始,用户可以随时撤消任何应用程序的权限,即使应用程序的API级别较低。无论您的应用针对什么API级别,您都应该测试您的应用,以确认它在错过所需权限时的行为是否正常。

除了清单中设置的权限之外,您还需要请求/检查运行时权限。您可以使用示例代码,或者......


快速解决方案,

  

转到“设置” - >应用 - &gt;(您的应用名称) - &gt;权限并启用相机权限。       完成,但不推荐用于最终产品

然后再次尝试您的应用。应该现在正在工作:D

答案 1 :(得分:-1)

尝试添加相机ID,例如

Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);