上篇文章介绍了HarmonyOS NEXT如何保存base64文件到download目录下,本次介绍如何保存base64图片到图库,网络图片保存方式大同小异,先下载图片,然后再保存
phAccessHelper.showAssetsCreationDialog
参考官方文档'
完整代码如下
//模拟数据
param ="{\"dataBase64\":\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA4QAAACiCAYAAAAZUwONAAAAAXNSR0IArs4c6QAAHWFJREFUeF7t3QusHFX9B/CDQdKqYCwiggELpjFS8IGaohXaYgCDBnxUbCUVovUFCKQxGvFZXokYIygGBTWFGGpiqtCYCohiiVURREUoj6YRW6BYKU2lAq0x/ec3cfY/d9m9u927OzO98znJTXt3Z+ac85lTki/nzJy9du3atSspBAgQIECAAAECBAgQINA4gb0Ewsbdcx0mQIAAAQIECBAgQIBAJiAQGggECBAgQIAAAQIECBBoqIBA2NAbr9sECBAgQIAAAQIECBAQCI0BAgQIECBAgAABAgQINFRAIGzojddtAgQIECBAgAABAgQICITGAAECBAgQIECAAAECBBoqIBA29MbrNgECBAgQIECAAAECBARCY4AAAQIECBAgQIAAAQINFRAIG3rjdZsAAQIECBAgQIAAAQICoTFAgAABAgQIECBAgACBhgoIhA298bpNgAABAgQIECBAgAABgdAYIECAAAECBAgQIECAQEMFBMKG3njdJkCAAAECBAgQIECAgEBoDBAgQIAAAQIECBAgQKChAgJhQ2+8bhMgQIAAAQIECBAgQEAgNAYIECBAgAABAgQIECDQUAGBsKE3XrcJECBAgAABAgQIECAgEBoDBAgQIECAAAECBAgQaKiAQNjQG6/bBAgQIECAAAECBAgQEAiNAQIECBAgQIAAAQIECDRUQCBs6I3XbQIECBAgQIAAAQIECAiExgABAgQIECBAgAABAgQaKiAQNvTG6zYBAgQIECBAgAABAgQEQmOAAAECBAgQIECAAAECDRUQCBt643WbAAECBAgQIECAAAECAqExQIAAAQIECBAgQIAAgYYKCIQNvfG6TYAAAQIECBAgQIAAAYHQGCBAgAABAgQIECBAgEBDBQTCht543SZAgAABAgQIECBAgIBAaAwQIECAAAECBAg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Ry0Vg2GuXWW28d8zzhxRdfnD1DWCzvfOc706pVq1of/fOf/0wvfelLa3oXNIsAAQIECBAgQIDA5BYQCGt6f/sJhL/+9a/TvHnz+u7B448/3nqeb7yTHnvssezZvtg6IkrMEl599dXZ20Ej9L3jHe/IPv/EJz6RzQDmW0hcd911rXAY319++eXpvPPOG1PV6aefnq6//vrWZ3/6059SLDNVCBAgQIAAAQIECBAoX0AgLN+8rxr7CYS/+93v0sc//vG+rhcHxX6CBxxwQF/HR9iM2bynn366dfwb3/jG9Mc//jEddNBB2bN/MTP4vOc9r/V9hMNzzz239Xu8YCZCY7EUZxfj86gn9jVUCBAgQIAAAQIECBAoX0AgLN+8rxr7CYR9XWgCB/31r3/N9iLMt5DILxXPDX71q19NM2bMGHP19qWmMav40Y9+dMwx8QbSCIp5+fnPf96acZxAU51KgAABAgQIECBAgMAAAgLhAGhlnFJ1IHzmmWey4PaVr3yltTl9sd+xHcU3vvGNLPDlL4XpJxB+8pOfTN/5zndal7rpppvSSSedVAapOggQIECAAAECBAgQaBMQCGs6JKoMhLHvYCwHzV/+EnsOxrN/V155Zfra1742RixmCj/zmc9kn8X3seF8XjotGY0A+b3vfa91TGxLEdtTKAQIECBAgAABAgQIlC8gEJZvPm6N8UxdlK1bt6b7778/xV593cooniHctWtXtkz0hz/8YVZt7DcYf89nAdesWZM+8IEPpEcffbTVrPzFMHHcokWLWp93eqlMXC/2McyLzelrNgA1hwABAgQIECBAoFECAmENbncsy4yZsjwMRpMidMWbPscLhKN4y2i8NCY2m89Lp5e+3Hjjjend735365iYNfz0pz+dfvWrX6W3v/3trc8vuuii9IUvfGGM8Mknn5ziucG8xB6HL3nJS2pwFzSBAAECBAgQIECAQPMEBMIK73mEraVLl44JgsXmHH/88dmbQcsssSF9bEyflw0bNqRDDjlkTBN27NiRpk2b1noD6dlnn50tF924cWM69NBDW8fmnxdPnjlzZlq7dm32UWw3EbOLCgECBAgQIECAAAEC1QgIhNW4Z7XmyzC7NSG2ffjZz35WagtXrlyZTj311FadDzzwQHr1q189pg3//e9/0/7775+2bduWfR6zmLENRZQ3v/nN6a677sr+Hs8GxsxnXrZv357taZiXL37xi+nCCy8stX8qI0CAAAECBAgQIEDg/wUEwopGQywTjdnB8cptt92W5s6dW2oL//a3v6XDDz+8VWfMGMby1WKJGb6Y6ctLLCE95ZRTsl+vueaaFHsN5uXxxx9PBx54YPbr7bffPmbPwfvuuy8dccQRpfZPZQQIECBAgAABAgQICISVj4Fes4MRBCMQVlHiTaGxBDTKUUcdlT3z94pXvCL7/d///ndavHhxiqAY5dhjj0233npr2meffbLfYxZw3rx5rVnCmAWM4Ltz585sKWq8PTXKkiVL0te//vUquqdOAgQIECBAgAABAgT+J2CGsIKh0OtlMFWGwTz0xfN/1157baYTzwWedtppacqUKVk4jBfPRDnhhBPSsmXL0sEHHzxG8cEHH0zz589P9957b/b52972tmx5aWx0HyWCYWw9EXsZKgQIECBAgAABAgQIVCcgEFZk323J6Je//OVsM/iqS2w/Ec//xYzeb3/72xRLSaNMnz49zZo1K73rXe/KAuHee+/dsanx9tDrr78+3XDDDdlLZCL8veENb0gLFizInlHsdl7V/VY/AQIECBAgQIAAgSYJCIQV3e2YJYyf/KUrc+bMyZ4XLPuZwYq6r1oCBAgQIECAAAECBGogIBDW4CZoAgECBAgQIECAAAECBKoQEAirUFcnAQIECBAgQIAAAQIEaiAgENbgJmgCAQIECBAgQIAAAQIEqhAQCKtQVycBAgQIECBAgAABAgRqICAQ1uAmaAIBAgQIECBAgAABAgSqEBAIq1BXJwECBAgQIECAAAECBGogUOtAGPvx1WFPvhrcJ00gQIAAAQIECBAgQIDA0AVqGwjnzZuX7dNXl43ahy7vggQIECBAgAABAgQIEKhYoJaBMGYFly5d2qIRCiseJaonQIAAAQIECBAgQGBSCtQuEMasYMwOtpddu3ZNyhugUwQIECBAgAABAgQIEKhKoHaBMF8q2g5ilrCqIaJeAgQIECBAgAABAgQmq0CtAmH7UlGzhJN12OkXAQIECBAgQIAAAQJ1EKhVIN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IECAAAECBAgQINBQAYGwoTdetwkQIECAAAECBAgQICAQGgMECBAgQIAAAQIECBBoqIBA2NAbr9sECBAgQIAAAQIECBAQCI0BAgQIECBAgAABAgQINFRAIGzojddtAgQIECBAgAABAgQI/B8WlZTPhnLNzQAAAABJRU5ErkJggg==\",\"fileType\":\"png\",\"fileName\":\"\"}"
if (StringUtils.isStrNotBlank(param)) {
const h5CallBackBean: H5CallBackBean | null = JSONUtil.jsonToBean<H5CallBackBean>(H5CallBackBean, param)
//保存图片
let context = getContext(this);
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
const fileBase64 = h5CallBackBean.dataBase64
if (StringUtils.isStrNotBlank(fileBase64)) {
const base64: string = fileBase64.split(",")[1]
let applicationContext = getContext().getApplicationContext()
const numTemp: number = DateUtil.getTodayTime()
let filePath = applicationContext.filesDir + '/' + numTemp + "." + h5CallBackBean.fileType
if (UiUtils.saveBase64File(base64, filePath)) {
try {
// 获取需要保存到媒体库的位于应用沙箱的图片/视频uri
let srcFileUris: Array<string> = [
fileUri.getUriFromPath(filePath)
];
let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [
{
fileNameExtension: 'jpg',
photoType: photoAccessHelper.PhotoType.IMAGE,
subtype: photoAccessHelper.PhotoSubtype.DEFAULT, // 可选
}
];
let desFileUris: Array<string> =
await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs);
console.info('showAssetsCreationDialog success, data is ' + desFileUris);
if (desFileUris.length > 0) {
FileUtil.copyFileSync(filePath,new fileUri.FileUri(desFileUris[0]).path)
try {
console.log(`context.fileDir ===> ${context.filesDir}`)
let srcFile: fs.File = fs.openSync(fileUri.getUriFromPath(filePath), fs.OpenMode.READ_ONLY);
let targetFile: fs.File = await fs.open(desFileUris[0], fs.OpenMode.READ_WRITE);
let bufSize = 14096;
let readSize = 0;
let buf = new ArrayBuffer(bufSize);
let readOptions: ReadOptions = { offset: readSize, length: bufSize };
let readLen = fs.readSync(srcFile.fd, buf, readOptions);
while (readLen > 0) {
readSize += readLen;
fs.writeSync(targetFile.fd, buf, { length: readLen });
readOptions.offset = readSize;
readLen = fs.readSync(srcFile.fd, buf, readOptions);
}
fs.closeSync(srcFile);
fs.closeSync(targetFile);
ToastUtil.showToast("保存成功")
if (FileUtil.accessSync(filePath)) {
FileUtil.unlink(filePath)
}
} catch (error) {
console.error(`createAssetByIo :: error , msg is ${error} `);
}
} else {
ToastUtil.showToast("您拒绝了保存图片")
}
} catch (err) {
console.error('showAssetsCreationDialog failed, errCode is ' + err.code + ', errMsg is ' + err.message);
}
} else {
ToastUtil.showToast("保存失败,请稍后重试")
}
}
}
}
其中用到的第三方依赖和上篇文章一致
参考:HarmonyNext保存Base64文件到Download下-CSDN博客
效果如下
【其它问题】
关于授权窗,没显示图片缩略图的问题,官方给的答复是下载最新版本的IDE
目前暂时没解决