Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot open Camera An error occurred while connecting to camera: 0

Tags:

android

camera

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>

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

Thanks in advance.

like image 614
Jorge Peris Avatar asked Oct 31 '22 01:10

Jorge Peris


1 Answers

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

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

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

like image 59
Red Dango Avatar answered Nov 13 '22 01:11

Red Dango