无法打开相机连接到相机时出错:0
Cannot open Camera An error occurred while connecting to camera: 0
我开始开发一个应用程序,我需要使用我的 phone 的相机,当我使用方法 Camera.open() 时,无论是否使用 cameraId,它 returns 错误 "An error occurred while connecting to camera: 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>
我的phone的Android版本是6.0.1,是BQ Aquaris M5。
提前致谢。
也尝试添加相机 ID,例如
Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
运行 Marshmallow 的设备需要在运行时设置权限,这是我对另一个类似问题的回答 :)
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.
除了清单中设置的权限外,您还需要request/check 以获得运行时权限。您可以使用其中的示例代码,或者...
快速解决方案,
go to Settings-> Apps->(Your app name)->Permissions and enable the camera permission.
Done, although not recommended for final product
然后再次尝试您的应用。现在应该可以工作了:D
我开始开发一个应用程序,我需要使用我的 phone 的相机,当我使用方法 Camera.open() 时,无论是否使用 cameraId,它 returns 错误 "An error occurred while connecting to camera: 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>
我的phone的Android版本是6.0.1,是BQ Aquaris M5。
提前致谢。
也尝试添加相机 ID,例如
Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
运行 Marshmallow 的设备需要在运行时设置权限,这是我对另一个类似问题的回答
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.
除了清单中设置的权限外,您还需要request/check 以获得运行时权限。您可以使用其中的示例代码,或者...
快速解决方案,
go to Settings-> Apps->(Your app name)->Permissions and enable the camera permission. Done, although not recommended for final product
然后再次尝试您的应用。现在应该可以工作了:D