androidcordovaionic-frameworkcordova-plugins

cordova-plugin-camera: Getting "No Image Selected" error on Android 4.4 device


I'm working on an hybrid mobile app build with Ionic 1.x. Currently I'm dealing with an weird behavior related to cordova-plugin-camera on devices running Android 4.4 (KitKat - Also tested in 4.3). When I run the following code:

var options = {
    quality: 50,
    mediaType: Camera.MediaType.PICTURE,
    destinationType: Camera.DestinationType.FILE_URI,
    sourceType: Camera.PictureSourceType.CAMERA,
    allowEdit: false,
    encodingType: Camera.EncodingType.JPEG,
    saveToPhotoAlbum: false,
    correctOrientation: true
};

navigator.camera.getPicture(function (imageData) {
    console.log(imageData);
}, function (err) {
    console.log(err);
}, options);

The error callback is instantly fired and prints "No Image Selected" to console. That is: the camera app is opened and the error callback is fired while the camera is opened, before I take a picture.

But this same code works on devices with new Android versions (tested on an Android 7.0 device).

I'm using:

Have tried this solution, but getting the same result. So, how can I solve this issue? Am I forgetting something?

Thanks for the help.


Solution

  • After two days of working on this and 30 minutes after this post on StackOverflow, the issue was solved following the steps described in this GitHub comment:

    1. Add the following line to your config.xml: <preference name="AndroidLaunchMode" value="singleTop" />
    2. Remove the directory platforms/android/
    3. Run ionic cordova prepare android

    Seems that the issue is caused by the installation of cordova-plugin-background-mode plugin, which I'm using too.