微信小程序保存图片至相册(解决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){
        if(err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
          wx.showModal({
            title: '提示',
            content: '需要您授权保存相册',
            showCancel: false,
            success: modalSuccess => {
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
                  }else {
                    console.log('获取权限失败,给出不给权限就无法正常使用的提示')
                  }
                }
              })
            }
          })
        }      

         }
        })
      },
      fail: function (res) {
        
      }
    })
posted @ 2020-03-09 10:47  小灬和灬尚  阅读(8509)  评论(0编辑  收藏  举报