Android Question RuntimePermission - Camera

Johan Schoeman

Expert
Licensed User
Longtime User
A little bit confused here. I have a project that adds permission to use the Camera via a library (target SDK 26 with B4A 8.30)

B4X:
@Permissions(values={"android.permission.CAMERA".....

In Activity_Create of the B4A project I use inline Java code to check if the device has a front and back camera and also if a Flash is present.

B4X:
    Log("ID of Front Facing Camera = " & nativeMe.RunMethod("getFrontFacingCamera", Null))
    Log("ID of Back Facing Camera = " & nativeMe.RunMethod("getBackFacingCamera", Null))
    Log("Device has a flash = " & nativeMe.RunMethod("hasFlash", Null))
  
    If nativeMe.RunMethod("hasFlash", Null) = False Then
       Log("does not have a flash")
    End If

But the above is executed successfully BEFORE I check and request the camera permission. I only ask for the camera permission when the START button is pressed to start the scanner.

If I don't check and ask for the camera permission in the B4A project (with target SDK 26) then the B4A app "errors - fail to connect to camera service". But it still executes the inline Java code and reports correctly on the ID's of the front and back cameras as well as the device having a flash - BEFORE the app crashes (because I have disabled the code to check for camera permission).

Can someone perhaps explain this observation?

Rgds

JS
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
What I am trying to say in the above post is that the below code is executed successfully before the camera permission is checked and requested:

B4X:
#If Java

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.CameraInfo;
import android.content.pm.PackageManager;
import android.content.Context;


    public int CAMERA_FRONT = -1;
    public int CAMERA_BACK = -1;   
    private boolean hasflash = false;

    public int getFrontFacingCamera() {
        int cameraId = -1;
        // Search for the front facing camera
        // get the number of cameras       
        int numberOfCameras = Camera.getNumberOfCameras();
        // for every camera check       
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                cameraId = i;
                break;
            }
        }
        CAMERA_FRONT = cameraId;
        return cameraId;
    }

    /**
     * Get the ID of the back facing camera
     * If the device has a front and back camera then the ID = 0
     * If the device has a back facing camera only then the ID = 0
     * It will return the ID as -1 if there is no back facing camera
     */
    public int getBackFacingCamera() {
        int cameraId = -1;
        // Search for the back facing camera
        // get the number of cameras
        int numberOfCameras = Camera.getNumberOfCameras();
        // for every camera check
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
                cameraId = i;
                break;
            }
        }
        CAMERA_BACK = cameraId;
        return cameraId;
    }   
    
    public boolean hasFlash() {
        /*
         * First check if device is supporting a flashlight or not
         */
        hasflash = BA.applicationContext.getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        
         return hasflash;
    }   

#End If

The inline Java code uses the camera and reports correctly before the permission was checked and added....?
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
It will fail when you try to open the camera.

Thanks Erel. Just needed to understand it. Thought that accessing the camera in any manner whatsoever would cause a failure when the permission has not been granted. But it makes sense that it will only fail when one tries to open the camera.

JS
 
Upvote 0
Top