Search code examples
androidcordovacordova-pluginsonsen-uimonaca

Camera works in Monaca Debugger but not in Debug Build


So, I am working on a new app that uses the camera for real-time video OCR on Android. Anyway, the app works great in the Monaca debugger. When I then do a debug or release build, the camera will not work.

Upon initial load, the app prompts to allow camera but the permission response is permanently denied even after hitting allow. I have tried the Android permissions plugin, diagnostics plugin, and the custom config plugin, but neither can get around this or I am not using them correctly.

When the app is used in the debugger, I get prompted for 2 permission, both pertaining to the camera or storage, but with the debug build, I only get the one for the camera. Here is the initial check:

ons.ready(function(){ 
  cordova.plugins.diagnostic.isCameraPresent(function(present){
        console.log("Camera is: "+present)
        if(present) {
          cordova.plugins.diagnostic.isCameraAuthorized({
              successCallback: function(authorized){
                  if (authorized){
                    showToast("App is authorized access to the camera","green");
                  } else {
                    setPermissions();
                  }
              },
              errorCallback: function(error){
                  console.error("The following error occurred: "+error);
              }, 
              externalStorage: true
          });
        } else
            document.getElementById('msg').innerHTML='Camera is not present.';
    }, function(error){
        console.log('The following error occurred: '+error);
    });
});

function setPermissions(){
  cordova.plugins.diagnostic.requestCameraAuthorization({
      successCallback: function(status){
          showToast("Authorization request for camera use was " + (status == cordova.plugins.diagnostic.permissionStatus.GRANTED ? "granted":"denied"),(status == cordova.plugins.diagnostic.permissionStatus.GRANTED ? "green":"red"));
      },
      errorCallback: function(error){
          console.error(error);
      },
      externalStorage: true
  });
}

Solution

  • I solved this using the Cordova-Custom-Config plugin. I had tried it in the past but didn't read the docs close enough I guess. You need the following:

    <widget xmlns:android="http://schemas.android.com/apk/res/android">
    <platform name="android">
        <custom-config-file target="AndroidManifest.xml" parent="/*">
            <uses-permission android:name="android.permission.CAMERA" />
        </custom-config-file>
    </platform>
    

    This solves the permission issue within the release builds.