微信小程序部分安卓机型无法保存图片的问题解决办法
== 问题描述:项目中保存图片到本地相册的功能部分机型出现bug,测试发现问题在部分安卓机型上复现==
原因是:部分安卓手机调用 wx.downloadFile方法下载网络资源图片后 会出现下载的图片临时路径的扩展名是unknown。所以后续在调用wx.saveImageToPhotosAlbum 方法时候会报错:saveimagetophotosalbum:fail invalid file type
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b0ee679b62979e02f8e0a416d4f9de1c.png)
解决办法:
let fileName = new Date().valueOf();
wx.downloadFile({
url: _this.data.post1[_this.data.tap_index],
filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.jpg',//自定义临时资源路径
success: (res) => {
let filePath = res.filePath;
wx.saveImageToPhotosAlbum({
filePath,
success(res) {
console.log(res)
wx.showToast({
title: '已保存到相册',
icon: 'success',
duration: 2000,
})
},
fail(e) {
console.log(e)
wx.showToast({
title: '保存失败,请重试',
duration: 2000,
icon: 'none',
})
},
})
}
});