微信小程式儲存圖片到相簿許可權設定
阿新 • • 發佈:2020-04-11
微信小程式儲存圖片到相簿可以分成兩種:一種是儲存網路圖片,一種是儲存本地圖片
儲存網路圖片:
var url = '網路圖片路徑'; wx.downloadFile({ url: url,success: function (res) { var benUrl = res.tempFilePath; //圖片儲存到本地相簿 wx.saveImageToPhotosAlbum({ filePath: benUrl,//授權成功,儲存圖片 success: function (data) { wx.showToast({ title: '儲存成功',icon: 'success',duration: 2000 }) },//授權失敗 fail: function (err) { if (err.errMsg) {//重新授權彈框確認 wx.showModal({ title: '提示',content: '您好,請先授權,在儲存此圖片。',showCancel: false,success(res) { if (res.confirm) {//重新授權彈框使用者點選了確定 wx.openSetting({//進入小程式授權設定頁面 success(settingdata) { console.log(settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) {//使用者打開了儲存圖片授權開關 wx.saveImageToPhotosAlbum({ filePath: benUrl,success: function (data) { wx.showToast({ title: '儲存成功',duration: 2000 }) },}) } else {//使用者未開啟儲存圖片到相簿的授權開關 wx.showModal({ title: '溫馨提示',content: '授權失敗,請稍後重新獲取',}) } } }) } } }) } } }) } })
儲存本地圖片:
wx.chooseImage({ count: 1,// 預設9 sizeType: ['original','compressed'],// 可以指定是原圖還是壓縮圖,預設二者都有 sourceType: ['album','camera'],// 可以指定來源是相簿還是相機,預設二者都有 success: function (res) { // 返回選定照片的本地檔案路徑列表,tempFilePath可以作為img標籤的src屬性顯示圖片 console.log("choose image") console.log(res) var tempFilePath = res.tempFilePaths[0] wx.getImageInfo({ src: tempFilePath,success: function (res) { console.log("get image info") console.log(res) wx.saveImageToPhotosAlbum({ filePath: res.path,success(res) { console.log("儲存圖片成功") console.log(res) wx.showToast({ title: '儲存成功',duration: 2000 }) },fail(err) { console.log('失敗') console.log(err) if (err.errMsg == "saveImageToPhotosAlbum:fail cancel") { wx.openSetting({ success(settingdata) { console.log(settingdata) if (settingdata.authSetting["scope.writePhotosAlbum"]) { console.log('獲取許可權成功,給出再次點選圖片儲存到相簿的提示。') } else { console.log('獲取許可權失敗,給出不給許可權就無法正常使用的提示') } } }) } } }) } }) } })
為大家推薦現在關注度比較高的微信小程式教程一篇:《微信小程式開發教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。