|
@@ -314,6 +314,11 @@ export class FileHelper {
|
|
|
return fs.accessSync(path);
|
|
|
}
|
|
|
|
|
|
+ static getFileFd(uri: string) {
|
|
|
+ let file = FileHelper.openSync(uri, fs.OpenMode.READ_ONLY)
|
|
|
+ return file.fd
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 图片转base64
|
|
|
* @param photoUri
|
|
@@ -348,29 +353,37 @@ export class FileHelper {
|
|
|
* 保存图片到相册,需要注意权限
|
|
|
* @param pixmap
|
|
|
*/
|
|
|
- static async saveImage(pixmap: image.PixelMap) {
|
|
|
- let imageBuffer: ArrayBuffer = new ArrayBuffer(1)
|
|
|
- try {
|
|
|
- imageBuffer = await image.createImagePacker().packing(pixmap, { format: "image/png", quality: 100 })
|
|
|
- } catch (err) {
|
|
|
- ToolsHelper.showMessage('保存失败')
|
|
|
- console.error(`Invoke packingPixelMap2Jpg failed, err: ${JSON.stringify(err)}`)
|
|
|
- }
|
|
|
+ static saveImage(pixmap: image.PixelMap): Promise<string> {
|
|
|
+ return new Promise<string>(async (resolve, reject) => {
|
|
|
+
|
|
|
+ let imageBuffer: ArrayBuffer = new ArrayBuffer(1);
|
|
|
+ try {
|
|
|
+ imageBuffer = await image.createImagePacker().packing(pixmap, { format: "image/png", quality: 100 });
|
|
|
+ } catch (err) {
|
|
|
+ ToolsHelper.showMessage('保存失败');
|
|
|
+ console.error(`Invoke packingPixelMap2Jpg failed, err: ${JSON.stringify(err)}`);
|
|
|
+ reject()
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- const uri = await photoAccessHelper.getPhotoAccessHelper(GlobalContext.getContext())
|
|
|
- .createAsset(photoAccessHelper.PhotoType.IMAGE, 'png')
|
|
|
-
|
|
|
- fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
|
|
|
- .then(async (file) => {
|
|
|
- try {
|
|
|
- await fs.write(file.fd, imageBuffer)
|
|
|
- ToolsHelper.showMessage('保存成功')
|
|
|
- } finally {
|
|
|
- await fs.close(file.fd) // 确保文件关闭
|
|
|
- }
|
|
|
- }).catch(() => {
|
|
|
- ToolsHelper.showMessage('保存失败')
|
|
|
- })
|
|
|
+ const uri = await photoAccessHelper.getPhotoAccessHelper(GlobalContext.getContext())
|
|
|
+ .createAsset(photoAccessHelper.PhotoType.IMAGE, 'png');
|
|
|
+
|
|
|
+ fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
|
|
|
+ .then(async (file) => {
|
|
|
+ try {
|
|
|
+ await fs.write(file.fd, imageBuffer);
|
|
|
+ ToolsHelper.showMessage('保存成功');
|
|
|
+ } finally {
|
|
|
+ await fs.close(file.fd); // 确保文件关闭
|
|
|
+ }
|
|
|
+ resolve(uri);
|
|
|
+ }).catch(() => {
|
|
|
+ ToolsHelper.showMessage('保存失败');
|
|
|
+ reject()
|
|
|
+ return;
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|