微信小程序保存圖片到相冊權(quán)限設(shè)置
微信小程序保存圖片到相冊可以分成兩種:一種是保存網(wǎng)絡(luò)圖片,一種是保存本地圖片
保存網(wǎng)絡(luò)圖片:
var url = '網(wǎng)絡(luò)圖片路徑'; wx.downloadFile({ url: url, success: function (res) { var benUrl = res.tempFilePath; //圖片保存到本地相冊 wx.saveImageToPhotosAlbum({ filePath: benUrl, //授權(quán)成功,保存圖片 success: function (data) { wx.showToast({ title: '保存成功', icon: 'success', duration: 2000 }) }, //授權(quán)失敗 fail: function (err) { if (err.errMsg) {//重新授權(quán)彈框確認(rèn) wx.showModal({ title: '提示', content: '您好,請先授權(quán),在保存此圖片。', showCancel: false, success(res) { if (res.confirm) {//重新授權(quán)彈框用戶點擊了確定 wx.openSetting({//進入小程序授權(quán)設(shè)置頁面 success(settingdata) { console.log(settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) {//用戶打開了保存圖片授權(quán)開關(guān) wx.saveImageToPhotosAlbum({ filePath: benUrl, success: function (data) { wx.showToast({ title: '保存成功', icon: 'success', duration: 2000 }) }, }) } else {//用戶未打開保存圖片到相冊的授權(quán)開關(guān) wx.showModal({ title: '溫馨提示', content: '授權(quán)失敗,請稍后重新獲取', showCancel: false, }) } } }) } } }) } } }) } })
保存本地圖片:
wx.chooseImage({ count: 1,// 默認(rèn)9 sizeType: ['original', 'compressed'],// 可以指定是原圖還是壓縮圖,默認(rèn)二者都有 sourceType: ['album', 'camera'],// 可以指定來源是相冊還是相機,默認(rèn)二者都有 success: function (res) { // 返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的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: '保存成功', icon: 'success', 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('獲取權(quán)限成功,給出再次點擊圖片保存到相冊的提示。') } else { console.log('獲取權(quán)限失敗,給出不給權(quán)限就無法正常使用的提示') } } }) } } }) } }) } })
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解使用grunt完成requirejs的合并壓縮和js文件的版本控制
這篇文章主要介紹了詳解使用grunt完成requirejs的合并壓縮和js文件的版本控制 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03IE中document.createElement的iframe無法設(shè)置屬性name的解決方法
這篇文章主要介紹了IE中document.createElement的iframe無法設(shè)置屬性name的解決方法,需要的朋友可以參考下2015-09-09微信小程序 JS動態(tài)修改樣式的實現(xiàn)代碼
這篇文章主要介紹了微信小程序 JS動態(tài)修改樣式的實現(xiàn)代碼,原理是綁定數(shù)據(jù),然后動態(tài)的修改數(shù)據(jù),從而實現(xiàn)動態(tài)樣式的改變而已,需要的朋友可以參考下2017-02-02設(shè)置iframe的document.designMode后僅Firefox中其body.innerHTML為br
設(shè)置iframe的document.designMode為On可以讓其可編輯,一般用在富文本編輯器組件中。這里僅列出各瀏覽器差異2012-02-02JavaScript之創(chuàng)意時鐘項目(實例講解)
下面小編就為大家?guī)硪黄狫avaScript之創(chuàng)意時鐘項目。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10