小程序保存视频/图片到相册并且授权

授权允许访问相册
wx.getSetting({
success(res) {
if (!res.authSetting[‘scope.writePhotosAlbum’]) { // 未授权
wx.authorize({
scope: ‘scope.writePhotosAlbum’,
success: res => { // 授权成功
// 用户已经同意小程序使用相册,后续调用 wx.saveVideoToPhotosAlbum 接口不会弹窗询问
// todo 授权成功需要执行的逻辑
},
fail: error => { // 拒绝授权
wx.showModal({
title: ‘温馨提示’,
content: 若点击不授权,将无法使用保存${that.data.stateText}功能,
cancelText: ‘不授权’,
cancelColor: ‘#999’,
confirmText: ‘授权’,
confirmColor: ‘#00c6ff’,
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
console.log(res.authSetting)
// todo 授权成功需要执行的逻辑
}
})
} else if (res.cancel) {
console.log(‘用户点击取消’)
}
}
})
}
})
} else {
// todo 授权成功需要执行的逻辑
}
}
})

保存图片到相册
wx.downloadFile({
url: ‘’, // 保存图片的路径
header: {
‘content-type’: ‘application/json’
},
success: res => {
if (res.statusCode === 200) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.hideLoading()
wx.showToast({
title: ‘图片保存成功请前往相册查看’,
icon: ‘none’
})
},
fail: error => {
wx.showToast({
title: ‘图片保存失败’,
icon: ‘none’
})
}
})
}
},
fail: error => {
console.log(error)
wx.showToast({
title: ‘图片太大无法保存’,
icon: ‘none’
})
}
})

保存视频到相册
wx.downloadFile({
url: ‘’, // 下载视频路径
header: {
‘content-type’: ‘application/json’
},
success: res => {
if (res.statusCode === 200) {
wx.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
wx.hideLoading()
wx.showToast({
title: ‘视频保存成功请前往相册查看’,
icon: ‘none’
})
},
fail: error => {
wx.showToast({
title: ‘视频保存失败’,
icon: ‘none’
})
}
})
}
},
fail: error => {
console.log(error)
wx.showToast({
title: ‘视频太大无法保存’,
icon: ‘none’
})
}
})

发布了21 篇原创文章 · 获赞 3 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_40039641/article/details/104989961
今日推荐