简体   繁体   中英

How to check camera permissions before invoking camera.getPicture

I want to be sure the application has permission to access the camera before actually opening it so that if the application does not have permission, the user can be notified that they need to change their permissions in the OS.

The reason this is necessary is because when the user accidentally denies permission to the camera, he would have to navigate to the app permissions within the OS themselves to change the permission. Most users probably don't know about this, therefore I would like to let them know.

In the example below I would like to check if the application has permission to access the camera. If it doesn't, notify the user.
How can I do this?

        fromCamera: function (callback) {

            // PERMISSION CHECK HERE -> if camera permission is FALSE show an alert to notify the user
            navigator.notification.alert(
                "This app does not have access to the camera. Blabla do this blabla",
                ["Ok"]
            );

            if (callback === undefined) throw 'undefined callback parameter!';

            navigator.camera.getPicture(onCameraSuccess, onCameraFail, {
                quality: 90,
                encodingType: Camera.EncodingType.JPEG,
                saveToPhotoAlbum: true,
                allowEdit: false,
                correctOrientation: true,
                destinationType: Camera.DestinationType.FILE_URI
            });

            function onCameraSuccess(imageUri) {
                app.log('onCameraSuccess: ' + imageUri);
                callback([imageUri]);
            }
            function onCameraFail(message) {
                app.log('Failed because: ' + message);
                callback([]);
            }
        }

So... i'm a bit sorry to say that, but this can't be an issue with the camera plugin.

I did the following:

  1. cordova create cameracheck com.example.com cameracheck
  2. cd cameracheck
  3. cordova platform add ios
  4. cordova plugin add cordova-plugin-camera
  5. cordova plugin add cordova-plugin-console
  6. cordova build

After that i opened the application in XCode and edited the code to standard code.

<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received" onclick="openCamera()">Device is Ready</p>
        </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
</body>

openCamera() function

function openCamera() {
    navigator.camera.getPicture(onSuccess, onFail, { quality: 50,
                                destinationType: Camera.DestinationType.FILE_URI });

    function onSuccess(imageURI) {
        var image = document.getElementById('myImage');
        image.src = imageURI;
    }

    function onFail(message) {
        alert('Failed because: ' + message);
    }
}

I declined the camera access and closed the application. After i reopened it, and pressed the text to start the camera, i got a message which directly tells me, that there is no access to the camera. Further the Plugin asks me, whether i want to open the settings or if i just want to close the camera. See the screenshot below.

The issue has to be anywhere inside your code, or you are may using a deprecated camera plugin? Did you try to update it?

在此处输入图片说明

For checking the permission of the camera roll before using cordova-camera-roll etc use : https://github.com/berliner/cordova-picture-access

Install by using cli with:

cordova plugin add https://github.com/berliner/cordova-picture-access.git

Then in code:

window.plugins.pictureAccess.checkAccess(
  function() {
    // Go ahead and access the camera roll here



  },
  function() {
    // Inform the user that he has to give permission for access.
    // Ideally, ask for permission and try again.

    $cordovaDialogs.confirm('Access to the camera roll is switched off, please enable it in app settings to continue.', 'Whoops', ['OK','Settings'])
    .then(function(result) {

        if(result == 1) {
            // ok tapped no action

        }
        else if (result == 2) {
            // settings tapped, redirect
            cordova.plugins.settings.open();

        }
    });

From this I have created a repo for checking camera permissions...

For checking the permission of the camera before using capture or video capture etc use : https://github.com/antonfire/cordova-camera-access.git

Install by using cli with:

cordova plugin add https://github.com/antonfire/cordova-camera-access.git

Then in code:

window.plugins.cameraAccess.checkAccess(
  function() {
    // Go ahead and access the camera here



  },
  function() {
    // Inform the user that he has to give permission for access.
    // Ideally, ask for permission and try again.

    $cordovaDialogs.confirm('Access to the camera is switched off, please enable it in app settings to continue.', 'Whoops', ['OK','Settings'])
    .then(function(result) {

        if(result == 1) {
            // ok tapped no action

        }
        else if (result == 2) {
            // settings tapped, redirect
            cordova.plugins.settings.open();

        }
    });

Hope this saves time for someone.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM