微信小程序保存图片至相册(解决saveimagetophotosalbum:fail invalid file type的问题和saveImageToPhotosAlbum:fail auth deny的问题)

   const that = this
    let fileName = new Date().valueOf();
    let filePath = wx.env.USER_DATA_PATH + '/' + fileName + '.jpg'
    wx.downloadFile({
      url: srcUrl,//此处为接口返回的图片地址
      filePath: filePath,
      success: function (res) {
        // 保存图片到系统相册
        wx.saveImageToPhotosAlbum({
          filePath: filePath,
          success(data) {
            let fileMgr = wx.getFileSystemManager()
            fileMgr.unlink({
              filePath: filePath,
              success(){
                console.log(222);
              }
            })
          },
          fail(data){
            wx.showToast({
              title: data.errMsg,
              icon: 'none',
              duration: 2000
            })
            if (data.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("用户一开始拒绝了,我们想再次发起授权")
              console.log('打开设置窗口')
              wx.openSetting({
                success(settingdata) {
                  console.log(settingdata)
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
                  }else {
                    console.log('获取权限失败,给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          }
        })
      },
      fail: function (res) {
        
      }
    })

猜你喜欢

转载自www.cnblogs.com/wxl99180426/p/12447258.html