javascript - How to check camera permissions before invoking camera.getPicture - Stack Overflow

I want to be sure the application has permission to access the camera before actually opening it so tha

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([]);
            }
        }

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([]);
            }
        }
Share Improve this question edited Jul 24, 2015 at 10:00 Bryandh asked Jul 24, 2015 at 9:55 BryandhBryandh 5291 gold badge6 silver badges22 bronze badges 5
  • And why actualy do you want to check this? This is automatically checked by the plugin when you open up the camera. – Sithys Commented Jul 24, 2015 at 10:00
  • 1 Correct, but if the user accidentally denies permission, the popup that asks for permission will not show the next time he tries to open the camera. – Bryandh Commented Jul 24, 2015 at 10:03
  • What about checking the permission and if there is no permission, give the oppertunity to the user, to directly open the settings app from that explicit application where he can allow the camera? =) – Sithys Commented Jul 24, 2015 at 10:13
  • That would be excellent indeed, my only problem is checking the permission, I don't know how. – Bryandh Commented Jul 24, 2015 at 10:16
  • I'm going to check this, give me just a few min. – Sithys Commented Jul 24, 2015 at 10:30
Add a ment  | 

2 Answers 2

Reset to default 1

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 .example. 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./berliner/cordova-picture-access

Install by using cli with:

cordova plugin add https://github./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./antonfire/cordova-camera-access.git

Install by using cli with:

cordova plugin add https://github./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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745328461a4622766.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信