昨天一个需求是在预览大图的时候,提供一个下载按钮,并且支持下载到相册中,网上搜出来的文档大部分都是可下载到沙箱中,在咨询过鸿蒙技术人员以后得知,可以使用保存组件(https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/savebutton-V5),使用的时候需要特别注意UI要求,不能把组件置于屏幕边缘,否则会权限申请失败,看代码中如何使用的
SaveButton()
.width(this.isShowRelevantMe ? '50%' : '100%')
.height(40)
.fontSize(12)
.backgroundColor(Color.Black)
.iconSize(16)
.onClick(async (event: ClickEvent, result: SaveButtonOnClickResult) => {
if (result === SaveButtonOnClickResult.SUCCESS) {
const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
// 免去权限申请和权限请求等环节,获得临时授权,保存对应图片
saveHttpPhoto(this.fileList[this.imageIndex].filePath, context)
} else {
Toast