代码:
saveImage(imgSrc, content = "是否允许获取保存相册权限", failTip = '暂无图片', successTip = "已保存到本地相册,请打开相册扫描") {
uni.getImageInfo({
src: imgSrc,
success: function(sres) {
console.log('图片信息', sres.path);
uni.saveImageToPhotosAlbum({
filePath: sres.path,
// 保存成功,直接给出提示
success: (res) => {
uni.showModal({
title: '提示',
content: successTip,
showCancel: false,
confirmText: "好的"
})
},
// 保存失败,判断是否授权,未授权则调用授权,否则弹出失败信息
fail(err) {
console.error(err);
if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg ===
"saveImageToPhotosAlbum:fail authorize no response" || err.errMsg ===
"saveImageToPhotosAlbum:fail auth denied") { // 没有授权,重新授权,兼容iso和Android
uni.showModal({
title: '授权提示',
content: content,
success: (res) => {
if (res.confirm) { // 点击确定,则调用相册授权
uni.openSetting({
success(settingdata) {
if (settingdata.authSetting[
"scope.writePhotosAlbum"
]) {
console.log(
"获取权限成功,再次点击图片保存到相册"
)
uni.showToast({
title: '授权成功,请重试哦~'
});
} else {
console.log("获取权限失败")
uni.showToast({
title: '请确定已打开保存权限',
icon: "none"
});
}
}
})
}
}
})
} else if (err.errMsg === "saveImageToPhotosAlbum:fail file not found" ||
err.errMsg ===
"saveImageToPhotosAlbum:fail file not exists" || err.errMsg ===
"saveImageToPhotosAlbum:fail get file data fail"
) { // 无图片,则提示
uni.showToast({
title: failTip,
icon: "none"
});
}
}
})
}
})
},