uniapp App 保存无后缀的图片到相册
问题描述:无后缀直接用uni.downloadFile和uni.saveImageToPhotosAlbum会报错
报错内容 saveImageToPhotosAlbum:fail UNKOWN ERROR3
解决方案:将文件重命名
uni.downloadFile({ url: this.url, success: async res => { if (res.statusCode === 200) { // 搞一个新的名字,这个自己随便弄一下,把https去了,后面自己加个后缀,xxx.png,xxx.jpg let name = this.getOldName(this.url); // 重命名的方法 let tempFilePath = await this.changeName(res.tempFilePath, name); uni.saveImageToPhotosAlbum({ filePath: tempFilePath, success: () => { // 已保存至相册,搞个提示 }, fail: err => { // 保存失败,搞个提示 } }); } }, fail: err => { //搞个提示 } });
修改文件名的方法
/** * 修改文件名称 */ changeName(url, newFileName) { return new Promise(resolve => { plus.io.resolveLocalFileSystemURL(url, entry => { entry.getParent(_oldFile => { entry.moveTo(_oldFile, '/' + newFileName, newFilePath => { console.log('newFilePath', newFilePath.fullPath); resolve(newFilePath.fullPath); }); }); }); }); },