Android Question RuntimePermission - Camera

Discussion in 'Android Questions' started by Johan Schoeman, Jun 17, 2018.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed 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)

    Code:
    @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.

    Code:
    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: Jun 17, 2018
  2. Johan Schoeman

    Johan Schoeman Expert Licensed 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:

    Code:
    #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....?
     
  3. Erel

    Erel Administrator Staff Member Licensed User

    It will fail when you try to open the camera.

    Why do you using inline Java for it? Everything that you need is available in CameraEx or Camera2 libraries.
     
  4. Johan Schoeman

    Johan Schoeman Expert Licensed User

    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
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice