uni-app uni.saveImageToPhotosAlbum()在鸿蒙next中保存无效
uni-app uni.saveImageToPhotosAlbum()在鸿蒙next中保存无效
项目信息 | 详情 |
---|---|
产品分类 | uniapp/App |
PC开发环境操作系统 | Windows |
PC开发环境操作系统版本号 | Win11 |
HBuilderX类型 | 正式 |
HBuilderX版本号 | 4.56 |
手机系统 | HarmonyOS NEXT |
手机系统版本号 | HarmonyOS NEXT Developer Preview |
手机厂商 | 华为 |
手机机型 | mate60pro |
页面类型 | vue |
vue版本 | vue3 |
打包方式 | 离线 |
项目创建方式 | HBuilderX |
示例代码:
// #ifdef APP-HARMONY
//利用plus完成保存到相册
let that = this
uni.downloadFile({
url: this.wximage,
success: function(res) {
///data/storage/el2/base/temp/HBuilder/download/1742005312684
console.log("下载成功",res);
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
// console.log(res);
uni.showToast({
title: '保存成功!',
icon: 'none'
})
},
fail(res) {
console.log(res);
uni.showToast({
title: '保存失败!',
icon: 'none'
})
},
});
},
fail: function() {
console.log('fail')
}
})
// #endif
操作步骤:
// #ifdef APP-HARMONY
//利用plus完成保存到相册
let that = this
uni.downloadFile({
url: this.wximage,
success: function(res) {
///data/storage/el2/base/temp/HBuilder/download/1742005312684
console.log("下载成功",res);///data/storage/el2/base/temp/HBuilder/canvas/1742005566471_3.png
that.getImageInfo(res.tempFilePath).then(res =>{
console.log("获取图片信息",res);
});
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success(res) {
// console.log(res);
uni.showToast({
title: '保存成功!',
icon: 'none'
})
},
fail(res) {
console.log(res);
uni.showToast({
title: '保存失败!',
icon: 'none'
})
},
});
},
fail: function() {
console.log('fail')
}
})
// #endif
预期结果:
保存成功
实际结果:
保存失败报错
bug描述:
saveImageToPhotosAlbum:fail save error, code: -2004
更多关于uni-app uni.saveImageToPhotosAlbum()在鸿蒙next中保存无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于uni-app uni.saveImageToPhotosAlbum()在鸿蒙next中保存无效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
根据描述,这个问题是由于HarmonyOS NEXT的权限机制变更导致的。在鸿蒙NEXT中,保存图片到相册需要额外配置权限声明。
解决方案:
- 在manifest.json中增加以下权限声明:
"app-permission": [
{
"name": "ohos.permission.WRITE_IMAGEVIDEO"
}
]
- 代码中需要动态请求权限:
// 在保存前先请求权限
uni.requestPermissions({
permissions: ['ohos.permission.WRITE_IMAGEVIDEO'],
success: (res) => {
if(res.granted && res.granted.length > 0) {
// 权限已授予,执行保存操作
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success() {
uni.showToast({title: '保存成功'});
},
fail() {
uni.showToast({title: '保存失败'});
}
});
}
}
});