當前鴻蒙開發(fā)中如果要保存圖片數據到系統(tǒng)相冊指蚜,需要basic權限,而不是我們正常使用Normal權限口蝠,一些應用為了避免申請權限的麻煩器钟,或者根本沒有理由申請basic權限,可以將圖片數據保存到系統(tǒng)文件中
//圖片資源壓縮處理
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 ---------------
后面會繼續(xù)補充不足之處妙蔗。