如何使用离子将使用URL的图像保存到ios / android画廊

时间:2018-08-10 07:49:37

标签: android ios ionic-framework

我正在构建一个简单的应用程序,该应用程序从远程服务器获取图像并显示它们,我正在尝试将图像保存到ios / android gallery。我正在使用filetrasnfert获取图像并在日志中(ios显示)

enter image description here

当我对图像进行录音时,它会下载并保存在I GESS的应用程序数据中,但未保存到图库中 如何将其保存到设备相册?

这是我的代码:slight_smile:

任何简单或最佳或替代的工作解决方案。

download(url,filename) {
    console.log("im called download");
    const fileTransfer: FileTransferObject = this.transfer.create();

    fileTransfer.download(url, this.file.dataDirectory +filename).then((entry) => {

        let filePath: string = entry.toURL();
        this.base64.encodeFile(filePath).then((base64File: string) => {
            console.log("im called encodeFile");

            var blob =base64File;
            this.savetogallry(blob);
        }, (err) => {
            console.log(err);
        });


        console.log('download complete: ' + entry.toURL());

    }, (error) => {
        // handle error
    });
}

base64toBlob(b64Data:string,contentType ?: string,sliceSize ?: number):Blob {         contentType = contentType || '';         sliceSize = sliceSize || 512;

    const byteCharacters: string = atob(b64Data);
    const byteArrays: Array<Uint8Array> = [];

    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        const slice: string = byteCharacters.slice(offset, offset + sliceSize);

        const byteNumbers: Array<number> = new Array(slice.length);
        for (let i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }

        const byteArray: Uint8Array = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
    }

    const blob: Blob = new Blob(byteArrays, {type: contentType});
    return blob;
}

savetogallry(blob){

    console.log("im called to save in library");
    this.base64ToGallery.base64ToGallery(blob, { prefix: '_img', mediaScanner:true }).then(
        res => console.log('Saved image to gallery ', res),
        err => console.log('Error saving image to gallery ', err)
    );

}

0 个答案:

没有答案