微信小程序保存圖片到相冊(cè)授權(quán)問題

問題

小程序保存圖片到相冊(cè)一直無響應(yīng),保存不成功

分析

小程序保存圖片到相冊(cè)主要用到以下三點(diǎn):
wx.downloadFile下載圖片
wx.openSetting授權(quán)
wx.saveImageToPhotosAlbum圖片保存到本地

查看wx.downloadFile請(qǐng)求正常響應(yīng),不過wx.saveImageToPhotosAlbum有報(bào)錯(cuò)信息,下面列出真機(jī)跟調(diào)試都出現(xiàn)的報(bào)錯(cuò)信息

err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response"

出現(xiàn)這報(bào)錯(cuò)信息,一般保存圖片用戶沒有授權(quán),可將授權(quán)方法wx.openSetting放入wx.showModal提示框中

wx.showModal({
  title: '提示',
  content: '需要您授權(quán)保存相冊(cè)',
  showCancel: false,
  success: res=> {
    wx.openSetting({
      success(res) {
        console.log("success", res)
      },
      fail(err) {
        console.log("fail", err)
      },
      complete(res) {
        console.log("finish", res)
      }
    })
  }
})
保存圖片完整代碼示例
    wx.downloadFile({
      url: e.currentTarget.dataset.contractimg, 
      success (res) {
        if (res.statusCode === 200) {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              util.hideToast();
              wx.showModal({
                title: '圖片保存成功',
                content: '圖片成功保存到相冊(cè)了',
                showCancel: false,
                confirmText: '好噠',
                confirmColor: '#5096cd',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用戶點(diǎn)擊確定');
                  }
                }
              })
            },
            fail (err) {
              util.hideToast()
              if (
                err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || 
                err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || 
                err.errMsg === "saveImageToPhotosAlbum:fail authorize no response"
              ) {
                wx.showModal({
                  title: '提示',
                  content: '需要您授權(quán)保存相冊(cè)',
                  showCancel: false,
                  success: res=> {
                    wx.openSetting({
                      success(res) {
                        if (settingdata.authSetting['scope.writePhotosAlbum']) {
                          wx.showModal({
                            title: '提示',
                            content: '獲取權(quán)限成功,再次點(diǎn)擊下載即可保存',
                            showCancel: false,
                          })
                        } else {
                          wx.showModal({
                            title: '提示',
                            content: '獲取權(quán)限失敗,將無法保存到相冊(cè)哦~',
                            showCancel: false,
                          })
                        }
                      },
                      fail(err) {
                        console.log("fail", err)
                      },
                      complete(res) {
                        console.log("finish", res)
                      }
                    })
                  }
                })
              } else if (err.errMsg === "saveImageToPhotosAlbum:fail cancel") {
                wx.showModal({
                  title: '提示',
                  content: '取消了保存圖片,再次點(diǎn)擊下載即可保存',
                  showCancel: false,
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: err.errMsg,
                  showCancel: false,
                })
              }
            }
          })
        } else {
          util.hideToast()
          wx.showModal({
            title: '提示',
            content: "圖片下載失敗",
            showCancel: false,
          })
        }
      },
      fail () {
        util.hideToast()
        wx.showModal({
          title: '提示',
          content: "圖片下載失敗",
          showCancel: false,
        })
      }
    })
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容