cordova图片下载
标签:
JavaScript
我们将介绍两种方式将照片保存到本地
这种方式会有一个弊端就是图片在相册里面找不到需要刷新一下系统目录或者重启一下手机才能在相册里面查看到图片但是可以下载动图
vm.url = encodeURI(baseConfig.imgPath + vm.nowImgs[vm.nowImgIndex]); vm.fileName = vm.nowImgs[vm.nowImgIndex].split("/").pop(); var fileTransfer = new FileTransfer(); var trustHosts = true; var options = { headers: { "Authorization": 'access_token' } }; //Android: vm.fileURL = cordova.file.externalRootDirectory; window.resolveLocalFileSystemURL(vm.fileURL, function (fileEntry) { fileEntry.getDirectory("Giti", {create: true, exclusive: false}, function (fileEntry) { fileEntry.getDirectory("img", {create: true, exclusive: false}, function (fileEntry) { fileTransfer.download( vm.url, fileEntry.toURL() + vm.fileName, function (success) { }, function (error) { }, options, trustHosts ); }, function () { publicMethod.showLoadingNoBackdrop('img文件夹创建失败'); }); }, function () { publicMethod.showLoadingNoBackdrop('Giti文件夹创建失败'); }); }); // iOS vm.fileURL = cordova.file.documentsDirectory + vm.fileName; window.requestFileSystem(LocalFileSystem.PERSISTENT, 5 * 1024 * 1024, function (fs) { fs.root.getDirectory('Giti', {create: true, exclusive: false}, function (directory) { fileTransfer.download( vm.url, directory.nativeURL + vm.fileName, function (success) { }, function (error) { }, trustHosts, options ); }); });
这种方法可以直接在相册里面看到图片但是动图看不到,下面是参考代码
function keepImg(){ var success = function (msg) { }; var error = function (err) { }; saveImageToPhone(vm.url, success, error); } function saveImageToPhone(url, success, error) { var canvas, context, imageDataUrl, imageData; var img = new Image(); img.src = url; img. = function () { console.log('Img size: ' + img.naturalWidth + 'x' + img.naturalHeight); canvas = document.createElement('canvas'); canvas.width = img.width; canvas.height = img.height; context = canvas.getContext('2d'); context.drawImage(img, 0, 0); try { imageDataUrl = canvas.toDataURL('image/jpeg', 1.0); imageData = imageDataUrl.replace(/data:image\/jpeg;base64,/, ''); cordova.exec( success, error, 'Canvas2ImagePlugin', 'saveImageDataToLibrary', [imageData] ); } catch (e) { error(e.message); } }; try { img.src = url; console.log("153" + img.src); } catch (e) { console.log("156" + img.src); error(e.message); } }
作者:Nickyzhang
链接:https://www.jianshu.com/p/d298ef375822
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦