微信小程序保存到相册
限微信小程序使用:
XML/HTML Code复制内容到剪贴板
- savePoster() {
- uni.getSetting({
- success: (res) => {
- if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
- this.saveImageToPhotosAlbum();
- } else {
- uni.authorize({ //如果没有授权,向用户发起请求
- scope: 'scope.writePhotosAlbum',
- success: () => {
- this.saveImageToPhotosAlbum();
- },
- fail: () => {
- uni.showToast({
- title: "请打开保存相册权限,再点击保存相册分享",
- icon: "none",
- duration: 3000
- });
- setTimeout(() => {
- uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
- success: (res2) => {
- // console.log(res2.authSetting)
- }
- });
- }, 3000);
- }
- })
- }
- }
- })
- },
- saveImageToPhotosAlbum() {
- uni.showLoading({
- title: '加载中',
- mask: true
- })
- uni.downloadFile({
- url: this.poster,
- success: function(res){
- console.log(res.tempFilePath);
- let filePath = res.tempFilePath;
- // console.log(res)
- uni.saveImageToPhotosAlbum({
- filePath: filePath,
- success: function(res2) {
- uni.hideLoading();
- uni.showToast({
- title: '保存成功,请从相册选择再分享',
- icon: "none",
- duration: 5000
- })
- },
- fail: function(err) {
- uni.hideLoading();
- console.log(err.errMsg);
- }
- })
- },
- fail: err => {
- uni.hideLoading();
- console.log(err)
- }
- })
- },
上一篇 微信小程序多点路线规划
下一篇 前端生成二维码