我开始开发一个应用程序,我需要使用手机的相机,当我使用 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。
提前致谢。
运行 Marshmallow 的设备需要在运行时设置权限,这是我在另一个类似问题中的回答here https://stackoverflow.com/questions/37837898/camera-access-error-on-some-android-devices/37838831#37838831 :)
From https://developer.android.com/training/permissions/requesting.html https://developer.android.com/training/permissions/requesting.html
注意:从 Android 6.0(API 级别 23)开始,用户可以随时撤销任何应用程序的权限,即使该应用程序的目标 API 级别较低。您应该测试您的应用程序,以验证它在缺少所需权限时是否能够正常运行,无论您的应用程序的目标 API 级别如何。
除了清单中设置的权限之外,您还需要请求/检查运行时的权限。那里有您可以使用的示例代码,或者...
快速解决方案,
进入设置->应用程序->(您的应用程序名称)->权限并启用相机权限。
完成,但不建议用于最终产品
然后再次尝试您的应用程序。现在应该可以工作了:D
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)