uni-app 在qq小程序真机环境下 uni.downloadFile 下载视频的文件路径是wxfile://开头,造成无法使用 uni.saveVideoToPhotosAlbum 保存进相册
uni-app 在qq小程序真机环境下 uni.downloadFile 下载视频的文件路径是wxfile://开头,造成无法使用 uni.saveVideoToPhotosAlbum 保存进相册
2 回复
在 uni-app
中,当你在 QQ 小程序中使用 uni.downloadFile
下载视频文件时,返回的文件路径会以 wxfile://
开头。然而,uni.saveVideoToPhotosAlbum
不支持直接使用 wxfile://
开头的路径来保存视频到相册。
解决方案
-
使用
uni.getFileSystemManager().saveFile
将文件保存到本地临时路径: 你可以先将下载的视频文件保存到本地临时路径,然后再使用uni.saveVideoToPhotosAlbum
将视频保存到相册。uni.downloadFile({ url: '视频的url', success: (res) => { if (res.statusCode === 200) { const tempFilePath = res.tempFilePath; // wxfile:// 开头的路径 // 将文件保存到本地临时路径 const fileManager = uni.getFileSystemManager(); fileManager.saveFile({ tempFilePath: tempFilePath, success: (saveRes) => { const savedFilePath = saveRes.savedFilePath; // 保存后的文件路径 // 保存视频到相册 uni.saveVideoToPhotosAlbum({ filePath: savedFilePath, success: () => { uni.showToast({ title: '保存成功' }); }, fail: (err) => { uni.showToast({ title: '保存失败', icon: 'none' }); console.error(err); } }); }, fail: (err) => { uni.showToast({ title: '文件保存失败', icon: 'none' }); console.error(err); } }); } }, fail: (err) => { uni.showToast({ title: '下载失败', icon: 'none' }); console.error(err); } });
-
直接使用
uni.saveVideoToPhotosAlbum
: 如果你不需要保存到本地临时路径,可以直接使用uni.saveVideoToPhotosAlbum
,但需要确保传入的路径是支持的格式。uni.downloadFile({ url: '视频的url', success: (res) => { if (res.statusCode === 200) { const tempFilePath = res.tempFilePath; // wxfile:// 开头的路径 // 直接保存视频到相册 uni.saveVideoToPhotosAlbum({ filePath: tempFilePath, success: () => { uni.showToast({ title: '保存成功' }); }, fail: (err) => { uni.showToast({ title: '保存失败', icon: 'none' }); console.error(err); } }); } }, fail: (err) => { uni.showToast({ title: '下载失败', icon: 'none' }); console.error(err); } });