Search code examples
ionic-frameworkpluginscordova-plugins

Ionic camera plugin getPicture ignored


This is freaking me. I have a simple app where you press a button and it take a picture, then the user can do other stuff but the problem is that sometime the getPicture function is ignored and camera never shows, here goes some info and simple code:

$ ionic info

Your system information:

Cordova CLI: 6.5.0 
Ionic CLI Version: 2.2.2
Ionic App Lib Version: 2.2.1
ios-deploy version: 1.9.1 
ios-sim version: 5.0.13 
OS: macOS Sierra
Node Version: v6.10.2
Xcode version: Xcode 8.3.2 Build version 8E2002

Installed platforms:
  android 6.0.0
  ios 4.3.1

plugins installed

$ ionic plugin ls
WARN: ionic.project has been renamed to ionic.config.json, please rename it.
com.googlemaps.ios 2.1.1 "Google Maps SDK for iOS"
cordova-plugin-camera 2.4.0 "Camera"
cordova-plugin-compat 1.1.0 "Compat"
cordova-plugin-console 1.0.3 "Console"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-file 4.3.1 "File"
cordova-plugin-file-transfer 1.6.1 "File Transfer"
cordova-plugin-geolocation 2.4.2 "Geolocation"
cordova-plugin-googlemaps 1.4.0 "phonegap-googlemaps-plugin"
cordova-plugin-screen-orientation 1.4.2 "Screen Orientation"
cordova-plugin-splashscreen 3.2.2 "Splashscreen"
cordova-plugin-statusbar 2.1.3 "StatusBar"
cordova-plugin-whitelist 1.2.2 "Whitelist"
ionic-plugin-keyboard 2.2.1 "Keyboard"

Then we have a HTML with this button

<button class="button button-full button-positive" ng-click="take_picture()">Take photo</button>

And the function into the controller

$scope.take_picture = function()
{
    screen.lockOrientation('portrait');
    var options = {
        quality: 25,
        destinationType: Camera.DestinationType.DATA_URL,
        sourceType: Camera.PictureSourceType.CAMERA,
        encodingType: Camera.EncodingType.JPEG,
        saveToPhotoAlbum: false,
        correctOrientation: true,
        allowEdit: false,
        targetHeight: 200,
        targetwidth: 200
    };

    $cordovaCamera.getPicture(options).then(function(imageURI) {
        $scope.image_uri = imageURI;
        $scope.marcar_gps();
        $cordovaCamera.cleanup(); 
    }, function(err) {
      console.log('error ',err);
      $cordovaCamera.cleanup();
      $ionicLoading.hide();
    });

};

Hope someone can help. Best!


EDIT

When camera is skipped on xCode console I get this: Warning: Attempt to present on which is already presenting


Solution

  • Avoid using DATA_URL destinationType and replace it with FILE_URI as it may consume so much memory on the old phones. When you're using FILE_URI as destinationType, you can use cordova file plugin to transfer image to the external/internal storage, then you can call the image src link to your local device url.


    EDIT

    camera plugin and orientation plugin wasn't up to date. So need it to update from versions 2.4.0 "Camera" and 1.4.2 "Screen Orientation" to 2.4.1 "Camera" and 2.0.1 "Screen Orientation"