How does Mini Program save pictures to photo albums
This article introduces the knowledge of "how Mini Program saves pictures to photo albums". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Official document
Authorization is required before saving the picture, so we authorize it first. The code is as follows:
/ / get album authorization wx.getSetting ({success (res) {if (! res.authSetting ['scope.writePhotosAlbum']) {wx.authorize ({scope:'scope.writePhotosAlbum',success () {console.log (' authorization'}})}})) / / this method can be written in app.js or when calling the save button. Obtain authorization
The first method is to save online pictures:
Var imgSrc = "http://yijiao.oss-cn-qingdao.aliyuncs.com/images/http://tmp/wx1b4e5e756cd48af1.o6zAJsws4grEQvYrWTjBigy-6QaU.0llhudiKSF2V955a1c48350d9328ef064b4d36d12746.jpg"wx.downloadFile({url: imgSrc,success: function (res) {console.log (res) / / Save the picture to the local wx.saveImageToPhotosAlbum ({filePath: res.tempFilePath,success: function (data) {wx.showToast ({title: 'saved successfully', icon: 'success',duration: 2000})}, fail: function (err) {console.log (err) If (err.errMsg = = "saveImageToPhotosAlbum:fail auth deny") {console.log ("when the user refused, initiate authorization again") wx.openSetting ({success (settingdata) {console.log (settingdata) if (settingdata.authSetting ['scope.writePhotosAlbum']) {console.log (' successfully get the permission, give the prompt to click on the picture to save to the album again.') } else {console.log ('failed to get permission, give a hint that it cannot be used normally without permission')}}, complete (res) {console.log (res);}})}) the second method: select the album image to save locally
Wx.chooseImage ({count:1,// default 9sizeType: ['original','compressed'], / / you can specify whether it is the original image or a compressed image. By default, both have sourceType: [' album','camera'], / / you can specify whether the source is a photo album or a camera, and both have success:function (res) {/ / return the local file path list of the selected photo. TempFilePath can be used as the src attribute of the img tag to display the picture 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 ("saved successfully") console.log (res) wx.showToast ({title:' saved successfully', icon:'success') Duration:2000})}, fail (err) {console.log ('failed') console.log (err) if (err.errMsg = = "saveImageToPhotosAlbum:fail cancel") {wx.openSetting ({success (settingdata) {console.log (settingdata) if (settingdata.authSetting ["scope.writePhotosAlbum"]) {console.log ('permission obtained successfully Give a hint to click the picture again to save to the album.') } else {console.log ('failed to get permission, give a hint that you cannot use it without permission')}))}) this is the end of the content of "how to save pictures to photo albums on Mini Program". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!