Download picture to gallery in Ionic2

I am trying to save / load an image to a gallery in an Ionic2 app. But I couldn't spare the gallery. Please correct me if there are any errors in my code.

Code1: Capture image

takeYourPicture(source) {
        if (source === 1) {
            this.camera.getPicture({
                quality: 100,
                destinationType: this.camera.DestinationType.FILE_URI,
                encodingType: this.camera.EncodingType.JPEG,
                mediaType: this.camera.MediaType.PICTURE,
                sourceType: this.camera.PictureSourceType.CAMERA,
                correctOrientation: true,
                saveToPhotoAlbum: true,
            }).then((imageData) => {
                this.base64Image = imageData;
                this.downloadLocation(this.base64Image);
            }, (error) => {

            })
        } else if (source === 0) {
            this.camera.getPicture({
                quality: 100,
                destinationType: this.camera.DestinationType.DATA_URL,
                encodingType: this.camera.EncodingType.JPEG,
                mediaType: this.camera.MediaType.PICTURE,
                sourceType: this.camera.PictureSourceType.PHOTOLIBRARY
            }).then((imageData) => {
                this.base64Image = imageData;
                this.downloadLocation(this.base64Image);
            }, (error) => {

            })
        }
    }

      

Code2: Loading location

 downloadLocation(img) {
    let storageDirectory: string = "";
    if (!this.platform.is('cordova')) {
        return false;
    }

    if (this.platform.is('ios')) {
        storageDirectory = cordova.file.documentsDirectory;
        console.log("Directory Ios=>"+storageDirectory);
    }
    else if (this.platform.is('android')) {
        storageDirectory = cordova.file.dataDirectory;
        console.log("Directory Android=>"+storageDirectory);
    }
    else {
        // exit otherwise, but you could add further types here e.g. Windows
        return false;
    }
    this.downloadImage(storageDirectory,img);
}

      

Code3: Upload Image

downloadImage(storageDirectory, img) {
        const fileTransfer: TransferObject = this.transfer.create();
        console.log("Base 64 0f Image:" + img);
        fileTransfer.download(img, storageDirectory + "sample.jpg").then((entry) => {
            const alertSuccess = this.alertCtrl.create({
                title: `Download Succeeded!`,
                subTitle: `Image was successfully downloaded to: ${entry.toURL()}`,
                buttons: ['Ok']
            });
            alertSuccess.present();
        }, (error) => {
            const alertFailure = this.alertCtrl.create({
                title: `Download Failed!`,
                subTitle: `Image was not successfully downloaded. Error code: ${error.code}`,
                buttons: ['Ok']
            });
            alertFailure.present();
        });
    }

      

where is transfer

added to the constructor as private transfer: Transfer

NB: It might be a duplicate of some other questions, please help me to solve this problem.

Thanks and Regards Anand

+3


source to share





All Articles