zoukankan      html  css  js  c++  java
  • 微信小程序点击按钮将图片保存到手机

     SaveCard: function(e) {
        let that = this;
        console.log('保存');
        var imgSrc = e.currentTarget.dataset.img;
        //获取相册授权
        wx.getSetting({
          success(res) {
            if (!res.authSetting['scope.writePhotosAlbum']) {
              wx.authorize({
                scope: 'scope.writePhotosAlbum',
                success() {
                  console.log('授权成功');
                  that.img(imgSrc)
                }
              })
            }else{
              that.img(imgSrc)
            }
          }
        })
      },
    img: function (imgSrc){
        var imgSrc = imgSrc;
        wx.downloadFile({
          url: imgSrc,
          success: function (res) {
            console.log(res); //图片保存到本地
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: function (data) {
                console.log(data);
                wx.showToast({
                  title: '保存成功',
                  duration: 2000
                })
              },
              fail: function (err) {
                console.log(err);
                if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                  wx.openSetting({
                    success(settingdata) {
                      console.log(settingdata)
                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
                        wx.showToast({
                          title: '图片已保存',
                          icon:'none',
                          duration:2000
                        })
                        console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
                      } else {
                        console.log('获取权限失败,给出不给权限就无法正常使用的提示')
                      }
                    }
                  })
                }
              }
            })
          }
        })
    
      },

    先授权,再保存

  • 相关阅读:
    Mybatis专栏文章整理成册《Mybatis进阶》!!!
    Mybatis的几种传参方式,你了解吗?
    HDU 1890
    POJ 2186
    HDU 2896
    POJ 1322
    POJ 1276
    POJ 1208
    POJ 1189
    POJ 1178
  • 原文地址:https://www.cnblogs.com/caoyuna/p/11724511.html
Copyright © 2011-2022 走看看