将图像下载到Ionic2中的图库

时间:2017-08-01 09:52:38

标签: angular typescript ionic2

我正在尝试在Ionic2应用程序中保存/下载图像到厨房。但我无法保存到画廊。如果我的代码中有任何错误,请更正我。

Code1:捕获图像

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:下载位置

 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:下载图片

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();
        });
    }

其中transfer作为private transfer: Transfer添加到构造函数中 注意:这可能与其他一些问题重复,请帮我解决。

谢谢和问候 阿南德

0 个答案:

没有答案