Search code examples
cordovaangularionic2cordova-pluginscrop

cordova-plugin-crop error on cropping 404


I am using Ionic 2 and cordova-plugin-crop to crop an image.

If I take a photo (pictureSourceType === navigator.camera.PictureSourceType.CAMERA), the cropping works fine, but if I pick an image from a folder (pictureSourceType === navigator.camera.PictureSourceType.SAVEDPHOTOALBUM), and try crop it, I get:

error on cropping 404

Here is my code:

takeThePhoto(pictureSourceType: any) {
    let options = {
        sourceType: pictureSourceType,
        destinationType: Camera.DestinationType.FILE_URI,
        quality: 50,
        targetWidth: 720,
        correctOrientation: true,
        encodingType: Camera.EncodingType.JPEG
    }
    if (pictureSourceType === navigator.camera.PictureSourceType.SAVEDPHOTOALBUM) {
        options.correctOrientation = false;
    }
    Camera.getPicture(options).then((imageURI) => {
        window['plugins'].crop.promise(imageURI, {
            quality: 75
        }).then(newPath => {
            alert('newPath = ' + newPath);
            return this.toBase64(newPath).then((base64Img) => {
                this.base64Image = base64Img;
            }).catch((error) => {
                console.error("ERROR -> " + JSON.stringify(error));
                alert("ERROR: " + JSON.stringify(error));
            });
        },
            error => {
                console.error("CROP ERROR -> " + JSON.stringify(error));
                alert("CROP ERROR: " + JSON.stringify(error));
            }
            ).catch((error) => {
                console.error("ERROR imageURI -> " + JSON.stringify(error));
                alert("ERROR imageURI: " + JSON.stringify(error));
            });
    },
        error => {
            // console.error("CAMERA ERROR -> " + JSON.stringify(error));
            // alert("CAMERA ERROR: " + JSON.stringify(error));
        }
    ).catch((error) => {
        console.error("ERROR getPicture -> " + JSON.stringify(error));
        alert("ERROR getPicture: " + JSON.stringify(error));
    });
}

Any help is appreciated.


Solution

  • As you already would have imported 'Camera' from 'ionic-native', try to replace:

      navigator.camera.PictureSourceType.SAVEDPHOTOALBUM
    

    with

    Camera.PictureSourceType.SAVEDPHOTOALBUM
    

    And also, try to replace

    window['plugins'].crop
    

    with

    import { Crop } from 'ionic-native';
    Crop.crop(.... )
    

    There is ionic2 usage example in plugin's github, if that helps: https://github.com/jeduan/cordova-plugin-crop