操作步骤:
目前使用的是SaveButton安全控件下载的图片和视频,图片可正常下载保存到图库,视频下载后图库中看不到,此操作并没有申请任何权限,想请教一下如何可以下载到图库中或者用什么方法可以做到,当前代码如下:
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;
// 免去权限申请和权限请求等环节,获得临时授权,保存对应图片
if (this.fileList[this.imageIndex].fileType === 0) {
DownloadUtils.saveHttpPhoto(this.fileList[this.imageIndex].filePath, context)
} else if (this.fileList[this.imageIndex].fileType === 1) {
DownloadUtils.saveHttpVideo(this.fileList[this.imageIndex].filePath, context)
}
} else {
Toast.showToast('下载失败')
}
})
//保存图片
static async saveHttpPhoto(url: string, context: common.UIAbilityContext) {
// 使用request下载图片并在回调函数中保存图片到相册
http.createHttp().request(url,
{
method: http.RequestMethod.GET,
connectTimeout: 60000,
readTimeout: 60000
},
async (error: BusinessError, data: http.HttpResponse) => {
if (error) {
console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`);
} else {
if (http.ResponseCode.OK === data.responseCode) {
let imageBuffer: ArrayBuffer = data.result as ArrayBuffer;
try {
// 获取相册路径
let helper = photoAccessHelper.getPhotoAccessHelper(context);
let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg')
let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE)
// 写入文件
await fileIo.write(file.fd, imageBuffer);
// 关闭文件
await fileIo.close(file.fd);
Toast.showToast('已保存至相册');
} catch (error) {
console.error("error is " + JSON.stringify(error))
Toast.showToast("error is " + JSON.stringify(error));
}
} else {
console.error("error occurred when image downloaded!")
Toast.showToast("error occurred when image downloaded!")
}
}
})
}
//保存视频
static async saveHttpVideo(url: string, context: common.UIAbilityContext) {
// 使用request下载图片并在回调函数中保存图片到相册
http.createHttp().request(url,
{
method: http.RequestMethod.GET,
connectTimeout: 60000,
readTimeout: 60000
},
async (error: BusinessError, data: http.HttpResponse) => {
if (error) {
console.error(`http reqeust failed with. Code: ${error.code}, message: ${error.message}`);
} else {
if (http.ResponseCode.OK === data.responseCode) {
let imageBuffer: ArrayBuffer = data.result as ArrayBuffer;
try {
// 获取相册路径
let helper = photoAccessHelper.getPhotoAccessHelper(context);
let uri = await helper.createAsset(photoAccessHelper.PhotoType.VIDEO, 'mp4')
let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE)
// 写入文件
await fileIo.write(file.fd, imageBuffer);
// 关闭文件
await fileIo.close(file.fd);
Toast.showToast('已保存至相册');
} catch (error) {
console.error("error is " + JSON.stringify(error))
Toast.showToast("error is " + JSON.stringify(error));
}
} else {
console.error("error occurred when image downloaded!")
Toast.showToast("error occurred when image downloaded!")
}
}
})
}
网络视频链接要先下载到沙箱,才能保存到相册。
参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-request-V5