【JS】微信小程序wx.saveImageToPhotosAlbum保存图片到相册保存不了?

在做微信小程序的时候需要将图片保存到相册,但是用微信提供的api wx.saveImageToPhotosAlbum 保存更本没有效果,网络图片和本地图片都试过了。下面是我的代码:
代码一:

wx.getImageInfo({

src: '../images/shareimg.jpg',

success: function (res) {

console.log(res.path)

wx.getSetting({

success(res) {

if (!res.authSetting['scope.writePhotosAlbum']) {

wx.authorize({

scope: 'scope.writePhotosAlbum',

success() {

wx.saveImageToPhotosAlbum({

filePath: res.path,

success(result) {

console.log(result)

}

})

}

})

}

}

})

}

})

代码二:

wx.downloadFile({

url: 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png',

success: function (res) {

wx.saveImageToPhotosAlbum({

filePath: res.tempFilePath,

success(result) {

console.log(result)

}

})

wx.saveFile({

tempFilePath: res.tempFilePath,

success: function (res) {

console.log(res.savedFilePath)

}

})

}

})

回答

试了下, 是可以的, 保存之前先download一下, 把返回的tempFilePath临时文件路径给wx.saveImageToPhotosAlbumfilePath参数

<!--wxml-->

<image class="img" src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg"></image>

<button bindtap='saveImg'>Save</button>

saveImg: function(){

wx.downloadFile({

url:'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',

success:function(res){

let path = res.tempFilePath

wx.saveImageToPhotosAlbum({

filePath: path,

success(res) {

console.log(res)

},

fail(res) {

console.log(res)

},

complete(res) {

console.log(res)

}

})

},fail:function(res){

console.log(res)

}

})

}

【JS】微信小程序wx.saveImageToPhotosAlbum保存图片到相册保存不了?

感觉没必要download,试了一下,这样也是可以的

 wx.getImageInfo({

src: 'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white_fe6da1ec.png',

success: function (ret) {

var path = ret.path;

wx.saveImageToPhotosAlbum({

filePath: path,

success(result) {

console.log(result)

}

})

}

})

我刚刚试了一下,调用长按保存图片api之前要先授权,就可以了

wx.getSetting({

success(res) {

if (!res.authSetting['scope.writePhotosAlbum']) {

wx.authorize({

scope: 'scope.writePhotosAlbum',

success() {

console.log('授权成功')

}

})

}

}

})

有可能是图片太大了?

你好,请问用小程序的相机拍照后怎么储存在本地呢?

getImageInfo res getSetting 的res 混了,命名成不同的名字就可以了

以上是 【JS】微信小程序wx.saveImageToPhotosAlbum保存图片到相册保存不了? 的全部内容, 来源链接: utcz.com/a/85351.html

回到顶部