微信小程序保存到相册
uni-app 2022-05-19 10:09:31

 限微信小程序使用:

XML/HTML Code复制内容到剪贴板
  1. savePoster() {  
  2.     uni.getSetting({  
  3.         success: (res) => {  
  4.             if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册  
  5.                 this.saveImageToPhotosAlbum();  
  6.             } else {  
  7.                 uni.authorize({ //如果没有授权,向用户发起请求  
  8.                     scope: 'scope.writePhotosAlbum',  
  9.                     success: () => {  
  10.                         this.saveImageToPhotosAlbum();  
  11.                     },  
  12.                     fail: () => {  
  13.                         uni.showToast({  
  14.                             title: "请打开保存相册权限,再点击保存相册分享",  
  15.                             icon: "none",  
  16.                             duration: 3000  
  17.                         });  
  18.                         setTimeout(() => {  
  19.                             uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册  
  20.                                 success: (res2) => {  
  21.                                     // console.log(res2.authSetting)  
  22.                                 }  
  23.                             });  
  24.                         }, 3000);  
  25.                     }  
  26.                 })  
  27.             }  
  28.         }  
  29.     })  
  30. },  
  31. saveImageToPhotosAlbum() {  
  32.     uni.showLoading({  
  33.         title: '加载中',  
  34.         mask: true  
  35.     })  
  36.     uni.downloadFile({  
  37.         url: this.poster,  
  38.         success: function(res){  
  39.             console.log(res.tempFilePath);  
  40.             let filePath = res.tempFilePath;  
  41.             // console.log(res)  
  42.             uni.saveImageToPhotosAlbum({  
  43.                 filePath: filePath,  
  44.                 success: function(res2) {  
  45.                     uni.hideLoading();  
  46.                     uni.showToast({  
  47.                         title: '保存成功,请从相册选择再分享',  
  48.                         icon: "none",  
  49.                         duration: 5000  
  50.                     })  
  51.                 },  
  52.                 fail: function(err) {  
  53.                     uni.hideLoading();  
  54.                     console.log(err.errMsg);  
  55.                 }  
  56.             })  
  57.         },  
  58.         fail: err => {  
  59.             uni.hideLoading();  
  60.             console.log(err)  
  61.         }  
  62.     })  
  63. },  

 

 

本文来自于:http://www.yoyo88.cn/study/uniapp/623.html

Powered by yoyo苏ICP备15045725号