当前鸿蒙开发中如果要保存图片数据到系统相册,需要basic权限,而不是我们正常使用Normal权限,一些应用为了避免申请权限的麻烦,或者根本没有理由申请basic权限,可以将图片数据保存到系统文件中
//图片资源压缩处理
let bitmap: ArrayBuffer;//图片数据
let packer: image.ImagePacker = image.createImagePacker();
let packerData = await packer.packing(bitmap, {
fornat: 'image/jpeg',
quality: 90
});
// 获取文件名称
let date = new Date();
let year: string = date.getFullYear().toString();
let mouth: string = this.addSpaceZero(date.getMonth() +1);
let day: string = this.addSpaceZero(date.getDate());
let hours: string = this.addSpaceZero(date.getHours());
let ninutes: string = this.addSpaceZero(date.getMinutes());
let seconds: string = this.addSpaceZero(date.getSeconds());
let inageTitle: string = 'IMG_' + year + mouth + day + '_' + hours + minutes + seconds + '.jpg';
hilog.info(0x0000,`saveQRNew imageTitle: ${imageTitle)`, '%{public}s');
//图片保存时option设置
let photoSaveOptions = new picker.PhoteSeveOptions();
photoSaveOptions.newFileNames =[imageTitle];
let abilityContext = getContext() as common.UIAbilityContext;
let photoPicker = new picker.PhotoVienPicker(abilityContext);
photoPicker.save(photoSaveOptions).then((photoSaveResult: Array<string>) =>{
hilog.info(0x0000,"photoPicker save success, uri: ${JSON.stringify(photoSaveResult)}', '%{public}s');
let uri: string = photoSaveResult[0] || '';
let file = fs.openSync(ur1, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);
fs.writesync(file.fd, packerData);
fs.closeSync(file.fd);
}).catch((err: BusinessError)>{
hilog.info(0x0000,"photoPicker save fail, err: S(JSON.stringify(err)}', '%{public}s');
});
addSpaceZero(timeNum: number): string {
return timeNum < 10 ? '0' + timeNum.toString() : timeNum.toString();
}
----------------- end ---------------
后面会继续补充不足之处。