CarlosC
CarlosC

Reputation: 11

How can i solve this error with camera permission on android?

I am a student and I was asked to make an app about image recognition, but when I try to use the camera I always get:

E/CameraService: Permission Denial: can't use the camera

The thing is that I add the permission on the manifest and I also used the on runtime permission request:

public void onScanButtonClicked(View view) {

    String[] permissos = {"android.permission.CAMERA"};

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this,
                permissos,
                MY_PERMISSIONS_REQUEST_CAMERA
        );
    }
      startActivity(new Intent(this, ScanActivity.class));
 }

Here is my manifest:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera2" />
    <uses-feature android:name="android.hardware.camera.any" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ScanActivity"
        android:label="@string/app_name"

        android:configChanges="orientation|screenSize"
        android:exported="true">
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature
            android:name="android.hardware.camera2"
            android:required="false" />
        <uses-feature
            android:name="android.hardware.camera.any"
            android:required="false" />


    </activity>
</application>

Upvotes: 1

Views: 9804

Answers (2)

Ildar Zaripov
Ildar Zaripov

Reputation: 551

I had similar problem. My manifest file was like

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        package="com.example.ildar.textureview">

        <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature android:name="android.hardware.camera" android:required="true"/>
        <uses-feature android:name="android.hardware.camera.autofocus" />

    ....

</manifest>

And the code that solved my problem:

if(ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){

            mCamera = Camera.open();
        }else{
            ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.CAMERA}, 0);
        }

In other words, if permission wasn't granted for me, I request it programmatically

Upvotes: 1

Hardik Amal
Hardik Amal

Reputation: 1353

Place the uses-permission and uses-feature outside the activity and application tag from your scan activity

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.camera.any" />
<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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ScanActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|screenSize"
        android:exported="true">
    </activity>
</application>

Also change the if loop to

public void onScanButtonClicked(View view) {

String[] permissos = {"android.permission.CAMERA"};

if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this,
            permissos,
            MY_PERMISSIONS_REQUEST_CAMERA
    );

}else{
    startActivity(new Intent(this, ScanActivity.class));
}

}

Upvotes: 3

Related Questions