From 49978315e797c26a7ce599c5745b2ecfaed54cea Mon Sep 17 00:00:00 2001 From: lloyd <353627866@qq.com> Date: Fri, 19 Sep 2025 21:07:24 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=AE=9E=E7=8E=B0=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=B8=8E=E4=BF=9D=E5=AD=98=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E3=80=91=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/common/utils/Utils.ets | 40 ++++++++------ .../main/ets/entryability/EntryAbility.ets | 10 +++- entry/src/main/ets/pages/Index.ets | 54 ++++++++----------- hvigor/hvigor-config.json5 | 2 +- oh-package.json5 | 2 +- 5 files changed, 57 insertions(+), 51 deletions(-) diff --git a/entry/src/main/ets/common/utils/Utils.ets b/entry/src/main/ets/common/utils/Utils.ets index 9d1f618..1b7828d 100644 --- a/entry/src/main/ets/common/utils/Utils.ets +++ b/entry/src/main/ets/common/utils/Utils.ets @@ -27,11 +27,16 @@ const TAG = 'UTILS'; * @returns Promise */ export async function copyImg2Sandbox(src: string, path: string): Promise { - let srcImage = fs.openSync(src, fs.OpenMode.READ_ONLY); - let destImage = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); - fs.copyFileSync(srcImage.fd, destImage.fd); - fs.closeSync(srcImage); - fs.closeSync(destImage); + try { + let srcImage = fs.openSync(src, fs.OpenMode.READ_ONLY); + let destImage = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + fs.copyFileSync(srcImage.fd, destImage.fd); + fs.closeSync(srcImage); + fs.closeSync(destImage); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x0000, 'copyImg2Sandbox', `copyImg2Sandbox failed, error code=${err.code}, message=${err.message}`); + } return path; } @@ -41,14 +46,19 @@ export async function copyImg2Sandbox(src: string, path: string): Promise */ -export function pixelMap2File(pixelMap: image.PixelMap | undefined, path: string): Promise { - const imagePacker = image.createImagePacker(); - let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); - let packOpts : image.PackingOption = { format: 'image/jpeg', quality: 98 }; - return imagePacker.packToFile(pixelMap, file.fd, packOpts).then(() => { - hilog.info(0x0000, TAG, 'packToFile succeed!'); - fs.closeSync(file.fd); - }).catch((error: BusinessError) => { - hilog.error(0x0000, TAG, 'packToFile failed, error: ' + JSON.stringify(error)); - }); +export async function pixelMap2File(pixelMap: image.PixelMap | undefined, path: string): Promise { + try { + const imagePacker = image.createImagePacker(); + let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 }; + imagePacker.packToFile(pixelMap, file.fd, packOpts).then(() => { + hilog.info(0x0000, TAG, 'packToFile succeed!'); + fs.closeSync(file.fd); + }).catch((error: BusinessError) => { + hilog.error(0x0000, TAG, 'packToFile failed, error: ' + JSON.stringify(error)); + }); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x0000, 'pixelMap2File', `pixelMap2File failed, error code=${err.code}, message=${err.message}`); + } } \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 51bdf85..aa9e6e9 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -16,11 +16,17 @@ import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; +import { BusinessError } from '@kit.BasicServicesKit'; export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + try { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x0000, 'testTag', `setColorMode failed, error code=${err.code}, message=${err.message}`); + } } onDestroy(): void { diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 3e835d9..ff39044 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -15,12 +15,11 @@ /** * 最佳实践:图片获取与保存实践 */ -import { image } from '@kit.ImageKit'; -import { PhotoPickerComponent, PickerController, photoAccessHelper, ReminderMode } from '@kit.MediaLibraryKit'; +import { BusinessError } from '@kit.BasicServicesKit'; import { cameraPicker, camera } from '@kit.CameraKit'; import { picker } from '@kit.CoreFileKit'; -import { promptAction } from '@kit.ArkUI'; -import { BusinessError } from '@kit.BasicServicesKit'; +import { image } from '@kit.ImageKit'; +import { PhotoPickerComponent, PickerController, photoAccessHelper, ReminderMode } from '@kit.MediaLibraryKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { copyImg2Sandbox, pixelMap2File } from '../common/utils/Utils'; @@ -185,18 +184,12 @@ struct Index { .height(40) .onClick(async () => { if (!this.pixelMap) { - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.no_pixel_map_alert'), - duration: 2000 - }); + this.showToast($r('app.string.no_pixel_map_alert'), 2000); return; } await pixelMap2File(this.pixelMap, this.path); - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.save_in_sandbox_success'), - duration: 2000 - }); + this.showToast($r('app.string.save_in_sandbox_success'), 2000); this.isShowSave = false; }) @@ -205,10 +198,7 @@ struct Index { .height(40) .onClick(async (event, result: SaveButtonOnClickResult) => { if (!this.pixelMap) { - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.no_pixel_map_alert'), - duration: 2000 - }); + this.showToast($r('app.string.no_pixel_map_alert'), 2000); return; } @@ -264,10 +254,7 @@ struct Index { .height(40) .onClick(() => { if (!this.imageUri) { - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.no_image_alert'), - duration: 2000 - }) + this.showToast($r('app.string.no_image_alert'), 2000); return; } @@ -292,10 +279,7 @@ struct Index { // [End image_proper] }); - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.get_image_info_success'), - duration: 2000 - }) + this.showToast($r('app.string.get_image_info_success'), 2000); }) Button($r('app.string.get_pixel_map')) @@ -303,19 +287,13 @@ struct Index { .height(40) .onClick(() => { if (!this.imageSource) { - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.no_image_info_alert'), - duration: 2000 - }) + this.showToast($r('app.string.no_image_info_alert'), 2000); return; } this.imageSource.createPixelMap().then((pixelMap: image.PixelMap) => { this.pixelMap = pixelMap; - this.getUIContext().getPromptAction().showToast({ - message: $r('app.string.get_pixel_map_success'), - duration: 2000 - }) + this.showToast($r('app.string.get_pixel_map_success'), 2000); }).catch((error: BusinessError) => { hilog.error(0x0000, TAG, 'createPixelMap failed, error: ' + JSON.stringify(error)); }) @@ -346,4 +324,16 @@ struct Index { .height('100%') .title($r('app.string.title')) } + + showToast(message: ResourceStr, duration?: number) { + try { + this.getUIContext().getPromptAction().showToast({ + message: message, + duration: duration + }); + } catch (error) { + let err = error as BusinessError; + hilog.error(0x0000, 'Index', `showToast failed, error code=${err.code}, message=${err.message}`); + } + } } \ No newline at end of file diff --git a/hvigor/hvigor-config.json5 b/hvigor/hvigor-config.json5 index 06b2783..5bebc97 100644 --- a/hvigor/hvigor-config.json5 +++ b/hvigor/hvigor-config.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.0", + "modelVersion": "5.0.5", "dependencies": { }, "execution": { diff --git a/oh-package.json5 b/oh-package.json5 index 8bf916d..2d9c74f 100644 --- a/oh-package.json5 +++ b/oh-package.json5 @@ -1,5 +1,5 @@ { - "modelVersion": "5.0.0", + "modelVersion": "5.0.5", "description": "Please describe the basic information.", "dependencies": { }, -- Gitee