Search code examples
androidandroid-camerax

Problem with the android.permission.CAMERA failure


I'm trying to implement the CameraX preview from the [Android developer site][1] I don't know why I'm getting this error even when I added the permissions in my manifests. When I open the QR scanner activity the preview is just black screen. I have updated to the latest version of cameraX and yet this issue was not solved. It seems that the permission for using camera was not granted. Please help me with it.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.two_locally">

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <activity android:name=".QRScanner"></activity>
        <activity android:name=".UpdateInfo" />
        <activity android:name=".SellPayActivity" />
        <activity android:name=".my_listing_details" />
        <activity android:name=".my_qr_code" />
        <activity android:name=".my_aacount" />
        <activity android:name=".Dashbord" />
        <activity android:name=".user_verify" />
        <activity android:name=".sign_in" />
        <activity android:name=".register_information" />
        <activity android:name=".creat_password" />
        <activity android:name=".verification_otp_number" />
        <activity android:name=".register_mobile_number" />
        <activity android:name=".intro_steper" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

QR scanner.java

package com.example.two_locally;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.Camera;
import androidx.camera.core.CameraSelector;
import androidx.camera.core.Preview;

import androidx.camera.lifecycle.ProcessCameraProvider;
import androidx.camera.view.PreviewView;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;

import com.google.common.util.concurrent.ListenableFuture;

import java.util.concurrent.ExecutionException;

public class QRScanner extends AppCompatActivity {
    ImageView back_image;
    private ListenableFuture<ProcessCameraProvider> cameraProviderFuture;
    PreviewView previewView;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_q_r_scanner);
        back_image = findViewById(R.id.back_img);
        previewView= findViewById(R.id.viewFinder);
        cameraProviderFuture = ProcessCameraProvider.getInstance(this);
        Log.i("Camerax", "preview view set up");
        back_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent r = new Intent(QRScanner.this, Dashbord.class);
                startActivity(r);
                finish();
            }
        });
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        cameraProviderFuture.addListener(() -> {
            try {
                ProcessCameraProvider cameraProvider = cameraProviderFuture.get();
                Log.i("Camerax", "The CameraProvider is working now in try");
                bindPreview(cameraProvider);
            } catch (ExecutionException | InterruptedException e) {
                // No errors need to be handled for this Future.
                // This should never be reached.
                Log.i("Camerax", "The Camerax catch portion");
                e.printStackTrace();
            }
        }, ContextCompat.getMainExecutor(this));

    }




    void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
        Preview preview = new Preview.Builder()
                .build();

        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();

        preview.setSurfaceProvider(previewView.getSurfaceProvider());
        Log.i("Camerax", "bindPreview is working");
        Camera camera = cameraProvider.bindToLifecycle((LifecycleOwner)this, cameraSelector, preview);

    }

}

Error messages

```1 890-7480/? W/ServiceManager: Permission failure: android.permission.CAMERA from uid=10269 pid=2714
2020-12-08 14:29:28.262 890-7480/? E/CameraService: Permission Denial: can't use the camera pid=2714, uid=10269
2020-12-08 14:29:28.263 2714-3336/com.example.cameraApp E/SequentialExecutor: Exception while executing runnable androidx.camera.camera2.internal.-$$Lambda$Camera2CameraImpl$uJgH7DGqogXqd1JLV8-47UNTCjM@a1e8d7d
    java.lang.SecurityException: validateClientPermissionsLocked:1251: Caller "com.example.cameraApp" (PID 10269, UID 2714) cannot open camera "0" without camera permission
        at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:827)
        at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:440)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:622)
        at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:590)
        at androidx.camera.camera2.internal.compat.CameraManagerCompatApi29Impl.openCamera(CameraManagerCompatApi29Impl.java:44)
        at androidx.camera.camera2.internal.compat.CameraManagerCompat.openCamera(CameraManagerCompat.java:203)
        at androidx.camera.camera2.internal.Camera2CameraImpl.openCameraDevice(Camera2CameraImpl.java:906)
        at androidx.camera.camera2.internal.Camera2CameraImpl.openInternal(Camera2CameraImpl.java:242)
        at androidx.camera.camera2.internal.Camera2CameraImpl.tryAttachUseCases(Camera2CameraImpl.java:709)
        at androidx.camera.camera2.internal.Camera2CameraImpl.lambda$attachUseCases$11$Camera2CameraImpl(Camera2CameraImpl.java:653)
        at androidx.camera.camera2.internal.-$$Lambda$Camera2CameraImpl$uJgH7DGqogXqd1JLV8-47UNTCjM.run(Unknown Source:4)
        at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.workOnQueue(SequentialExecutor.java:229)
        at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.run(SequentialExecutor.java:171)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:919)
     Caused by: android.os.ServiceSpecificException: validateClientPermissionsLocked:1251: Caller "com.example.two_locally" (PID 10269, UID 2714) cannot open camera "0" without camera permission (code 1)
        at android.os.Parcel.createException(Parcel.java:2101)
        at android.os.Parcel.readException(Parcel.java:2055)
        at android.os.Parcel.readException(Parcel.java:2003)
        at android.hardware.ICameraService$Stub$Proxy.connectDevice(ICameraService.java:583)
        at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:403)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:622) 
        at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:590) 
        at androidx.camera.camera2.internal.compat.CameraManagerCompatApi29Impl.openCamera(CameraManagerCompatApi29Impl.java:44) 
        at androidx.camera.camera2.internal.compat.CameraManagerCompat.openCamera(CameraManagerCompat.java:203) 
        at androidx.camera.camera2.internal.Camera2CameraImpl.openCameraDevice(Camera2CameraImpl.java:906) 
        at androidx.camera.camera2.internal.Camera2CameraImpl.openInternal(Camera2CameraImpl.java:242) 
        at androidx.camera.camera2.internal.Camera2CameraImpl.tryAttachUseCases(Camera2CameraImpl.java:709) 
        at androidx.camera.camera2.internal.Camera2CameraImpl.lambda$attachUseCases$11$Camera2CameraImpl(Camera2CameraImpl.java:653) 
        at androidx.camera.camera2.internal.-$$Lambda$Camera2CameraImpl$uJgH7DGqogXqd1JLV8-47UNTCjM.run(Unknown Source:4) 
        at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.workOnQueue(SequentialExecutor.java:229) 
        at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.run(SequentialExecutor.java:171) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:919) ```


  [1]: https://developer.android.com/training/camerax/preview

Solution

  • Please make sure you have accepted camera permission in your app. you can manually check this by going to Settings > Apps > app_name > Permissions

    and then setting all permissions to granted!

    also add this line in your manifest file:

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

    This just makes sure that the device has a camera. Specifying .any means that it can be a front camera or a back camera.