一开始我还还以为挺简单的,毕竟文档这么多,但是就是保存不了 经过半天的摸索 有点思路
我用的是uni-app,如果原生小程序就把uni改成wx
第一步点击判断是否授权 执行对应方法
preservationImg(img) {
let that = this
uni.authorize({
/* scope.writePhotosAlbum 类型是保存到相册 */
scope: 'scope.writePhotosAlbum',
success() {
/* 已授权进入 */
/* 保存图片到相册方法方法 */
that.imgApi(img);
},
complete(res) {
/* 判断如果没有授权就打开设置选项让用户重新授权 */
uni.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
/* 打开设置的方法 */
that.openInstall();
}
}
});
}
});
}
保存图片方法
imgApi(image) {
/* 获取图片的信息 */
uni.getImageInfo({
src: image,
success: function(image) {
/* 保存图片到手机相册 */
uni.saveImageToPhotosAlbum({
filePath: image.path,
success: function() {
uni.showModal({
title: '保存成功',
content: '图片已成功保存到相册',
showCancel: false
});
},
complete(res) {
console.log(res);
}
});
}
});
},
跳转设置重新授权方法
opensit() {
uni.showModal({
content: '没有授权保存图片到相册,点击确定去允许授权',
success: function(res) {
if (res.confirm) {
/* 打开设置的API*/
uni.openSetting({
success(res) {
console.log(res.authSetting);
}
});
} else if (res.cancel) {
uni.showModal({
cancelText: '取消',
confirmText: '重新授权',
content: '你点击了取消,将无法进行保存操作',
success: function(res) {
if (res.confirm) {
uni.openSetting({
success(res) {
/* 授权成功 */
console.log(res.authSetting);
}
});
} else if (res.cancel) {
console.log('用户不授权');
}
}
});
}
}
});
},