【问题标题】:Cannot open Camera An error occurred while connecting to camera: 0无法打开相机连接相机时出错:0
【发布时间】:2016-06-17 20:20:44
【问题描述】:

我开始开发一个应用程序,我需要使用手机的摄像头,当我使用方法 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。

提前致谢。

【问题讨论】:

    标签: android camera


    【解决方案1】:

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

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

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


    快速解决方案,

    进入设置->应用->(您​​的应用名称)->权限并启用相机权限。 完成,但不推荐用于最终产品

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

    【讨论】:

    • 我今天试过了,我的应用程序在没有改变任何东西的情况下运行......这很奇怪,因为我重新启动了我的电脑和手机几次但它没有工作,突然它就可以了 XD 我如您所说,检查了应用程序中的权限,并且至少现在允许使用相机。所以谢谢你的建议;)
    • 当然没问题 :) 如果可能的话,如果有帮助,请将我的帖子标记为已接受的答案,因为它可能会帮助其他有类似问题的人。在你的编程中一切顺利:D
    • 对不起,我是 stackoverflow 的新手,我不知道该怎么做 :)
    【解决方案2】:

    尝试同时添加相机 ID,例如

    Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 2021-11-08
      相关资源
      最近更新 更多