一、微信小程序保存视频到相册wx.saveVideoToPhotosAlbum()
功能描述
保存视频到系统相册。支持mp4视频格式。
参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
filePath | string | 是 | 视频文件路径,可以是临时文件路径也可以是永久文件路径 (本地路径) | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
注意:需要用户授权访问相册,才能保存成功。
二、使用案例代码
1.onload 获取授权
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
//提前发起授权申请
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: res => {
},
fail: res => {
wx.showToast({
title: '拒绝授权后,请点击授权相册',
})
}
})
},
2.点击事件,处理下载+保存到相册
//下载
downloadClick() {
//视频保存到相册跟图片保存到相册使用方式相同
//下载到本地,判断是否拥有权限
wx.getSetting({
withSubscriptions: true,
success: res => {
console.info(res.authSetting);
if (res.authSetting['scope.writePhotosAlbum']) {
wx.downloadFile({
url: 'http://train.khqianle.cn/image/video2.mp4',
success: res => {
//保存到相册
wx.saveVideoToPhotosAlbum({
filePath: res.tempFilePath,
success: res => {
console.info(res);
wx.showToast('视频保存到相册');
},
fail: res => {
wx.showToast({
title: '保存失败',
})
}
})
}
});
} else {
wx.showModal({
cancelColor: 'cancelColor',
title: '提示',
content: '请开启相册访问权限',
success: res => {
if (res.confirm)
wx.openSetting({
withSubscriptions: true,
})
}
})
}
}
})
},
更多: