KoenJ
KoenJ

Reputation: 1141

Cordova: camera.getPicture fails with "Camera cancelled"

I'm developing a cordova app (cli-5.2.0) in which pictures are taken using the cordova camera plugin (cordova-plugin-camera, version 1.2.0).

var camera = navigator.camera;
var cameraOptions = { 
  destinationType: Camera.DestinationType.DATA_URL
};

camera.getPicture(function(result) {
    alert('success');
  }, function(error) {
    alert('fail ' + error);
  }, cameraOptions);

This code works fine.. ..most of the time.
On a particular slow android 4.4.4 device, this only works fine like 9 out of 10 times.
In like 1 out of 10, the call fails saying 'Camera cancelled' (although it was not cancelled with the back button or something).

In that case, the failure callback 'Camera cancelled' is fired, and right after that, the Camera UI is shown nevertheless.
(if a picture is taken then, the success callback is not called).

It's hard to tell when this happens, but it looks like it happens more often if the device is app is more busy.
The log files do not show much more detail, except for things like:

Attempted to send a second callback for ID: Camera1337050609

However, these logs are also written if the call succeeds.


Any hints or help is much appreciated.

Thanks for your time,
Koen

update 1 (answer to comment)

ProjectDetailCtrl.prototype.takePhoto = function () {
    var _this = this;
    // this cameraService calls the code from above
    return this.cameraService.getPicture()
        .then(function (imageData) {
            _this.$log.debug('got camera data');
            // ..
        },
<!-- This is an angular application, the click handling goes like: -->
<md-button ng-click="vm.takePhoto()">
    Add photo
</md-button>

Upvotes: 2

Views: 5070

Answers (2)

user7783007
user7783007

Reputation:

My problem was that I had Angular material installed which provokes that the buttons and other click elements gets fired two times. To solve it I had to add this line in my app.js

.config(function($stateProvider, $urlRouterProvider, $mdThemingProvider, $mdGestureProvider) {

$mdGestureProvider.skipClickHijack();

Upvotes: 0

Nat
Nat

Reputation: 310

I have seen this problem occur under 2 conditions - 1) Low memory as what @Joerg has already mentioned, 2) Your events are cascaded, and are triggered twice. For instance, you have a onClick on an Icon, inside a div that also has another event trigger like an a-href or another onClick.

If you can post your code, it might help.

Update - 1: Definitely indicates a low memory situation. Hazarding a guess here... 1. Asynch call to Camera occurs 2. Low memory returns a Camera cancelled failure event 3. Camera does open later when enough free memory is available 4. Camera success event is not able to find the callback as the callback is already cleared after the earlier failure event, resulting in 'Second callback' scenario

Now, do you have this problem with the exact same photo/video each time?

The way you can probably avoid this is - not chain the call with a ".then" but actually do a regular async callback.

Also, you might want to check and guard against any earlier invocation of the 'camera service' is finished.

Upvotes: 1

Related Questions