簡體   English   中英

在調用camera.getPicture之前如何檢查相機權限

[英]How to check camera permissions before invoking camera.getPicture

我想確保在實際打開相機之前,該應用程序具有訪問相機的權限,這樣,如果該應用程序沒有權限,則可以通知用戶他們需要在操作系統中更改其權限。

之所以需要這樣做,是因為當用戶意外拒絕對攝像機的許可時,他將不得不導航到操作系統本身中的應用程序許可才能更改許可。 大多數用戶可能對此一無所知,因此我想讓他們知道。

在下面的示例中,我想檢查應用程序是否有權訪問相機。 如果不是,請通知用戶。
我怎樣才能做到這一點?

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

所以...我很遺憾地說,但這不是相機插件的問題。

我做了以下事情:

  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

之后,我在XCode中打開應用程序,並將代碼編輯為標准代碼。

<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 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);
    }
}

我拒絕了攝像頭的訪問並關閉了該應用程序。 重新打開它,然后按文本以啟動相機后,我收到一條消息,直接告訴我,無法訪問相機。 插件進一步詢問我是要打開設置還是只想關閉相機。 請參見下面的屏幕截圖。

問題必須在代碼內的任何地方,否則您可能正在使用不贊成使用的相機插件? 您是否嘗試更新它?

在此處輸入圖片說明

要在使用cordova-camera-roll等之前檢查相機膠卷的許可,請使用: https : //github.com/berliner/cordova-picture-access

通過使用cli進行安裝:

cordova插件添加https://github.com/berliner/cordova-picture-access.git

然后在代碼中:

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();

        }
    });

由此我創建了一個用於檢查相機權限的倉庫...

要在使用捕獲或視頻捕獲等之前檢查攝像機的許可,請使用: https : //github.com/antonfire/cordova-camera-access.git

通過使用cli進行安裝:

cordova插件添加https://github.com/antonfire/cordova-camera-access.git

然后在代碼中:

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();

        }
    });

希望這可以為某人節省時間。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM