先說(shuō)說(shuō)我遇到的問(wèn)題,直接使用uni.saveImageToPhotosAlbum報(bào)錯(cuò),提示信息為:fail not absolute path。
從官網(wǎng)可以看出來(lái),圖片文件路徑filePath不可以是網(wǎng)絡(luò)圖片路徑,只可以是臨時(shí)路徑或者說(shuō)是永久文件路徑。

所以我們可以用
uni.downloadFile將圖片下載到本地,然后將它返回的本地臨時(shí)文件保存到相冊(cè)就可以了。
1、完整的思路流程
1、首先查看用戶是否有‘’保存到相冊(cè)‘’的權(quán)限,uni.getSetting(獲取用戶的當(dāng)前設(shè)置);
2、如果用戶沒(méi)有授權(quán),發(fā)起授權(quán),uni.authorize(提前向用戶發(fā)起授權(quán)請(qǐng)求。調(diào)用后會(huì)立刻彈窗詢問(wèn)用戶是否同意授權(quán)小程序使用某項(xiàng)功能或獲取用戶的某些數(shù)據(jù),但不會(huì)實(shí)際調(diào)用對(duì)應(yīng)接口。如果用戶之前已經(jīng)同意授權(quán),則不會(huì)出現(xiàn)彈窗,直接返回成功。如果用戶之前拒絕了授權(quán),此接口會(huì)直接進(jìn)入失敗回調(diào),一般搭配uni.getSetting和uni.openSetting使用);
3、如果在授權(quán)過(guò)程中,用戶點(diǎn)擊了拒絕授權(quán),跳轉(zhuǎn)至設(shè)置頁(yè)面,引導(dǎo)用戶授權(quán);
4、保存圖片;
2、完整代碼
savePoster() {
let that = this;
uni.downloadFile({
url: this.poster,
success: res => {
if (res.statusCode === 200) {
that.filePath = res.tempFilePath;
//獲取授權(quán)列表,查看是否授權(quán)寫入相冊(cè)權(quán)限
uni.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success() {
that.saveimgtoAlbum()
}
})
} else {
that.saveimgtoAlbum()
}
}
})
}
}
})
},
saveimgtoAlbum() {
//圖片保存到本地
let that = this;
uni.showLoading();
uni.saveImageToPhotosAlbum({
filePath: that.filePath,
success: function(data) {
uni.hideLoading()
uni.showToast({
title: '保存成功',
icon: 'success',
duration: 2000
})
},
fail: function(err) {
uni.hideLoading();
if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
uni.openSetting({
success(settingdata) {
if (settingdata.authSetting['scope.writePhotosAlbum']) {
uni.showToast({
title: '您已授權(quán)成功,請(qǐng)重新保存收款碼',
icon: 'success',
duration: 2000
})
} else {
uni.showToast({
title: '尚未授權(quán),無(wú)法保存海報(bào)',
icon: 'none',
duration: 2000
})
}
}
})
}
}
})
}
}
原文轉(zhuǎn)載:https://blog.csdn.net/weixin_45563734/article/details/137873076,這里只做記錄使用