From 4a9f1a22873f4c8baa000b890a318d9b5a450ad2 Mon Sep 17 00:00:00 2001 From: ly Date: Tue, 6 May 2025 10:31:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BA=9F=E5=BC=83API=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/Index.ets | 33 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index fea4cf7..6f11892 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -13,11 +13,11 @@ * limitations under the License. */ +import { common } from '@kit.AbilityKit'; import { image } from '@kit.ImageKit'; import { PhotoPickerComponent, PickerController, photoAccessHelper, ReminderMode } from '@kit.MediaLibraryKit'; import { cameraPicker, camera } from '@kit.CameraKit'; import { picker } from '@kit.CoreFileKit'; -import { promptAction } from '@kit.ArkUI'; import { BusinessError } from '@kit.BasicServicesKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { copyImg2Sandbox, pixelMap2File } from '../common/utils/Utils'; @@ -27,8 +27,11 @@ const TAG = 'IMAGE_APP'; @Entry @Component struct Index { - @State path: string = getContext(this).filesDir + '/image.jpg'; - @State pixelMapPath: string = getContext(this).filesDir + '/pixelMap.jpg'; + UIContext = this.getUIContext(); + context = this.UIContext.getHostContext() as common.UIAbilityContext; + + @State path: string = this.UIContext.getHostContext()?.filesDir + '/image.jpg'; + @State pixelMapPath: string = this.UIContext.getHostContext()?.filesDir + '/pixelMap.jpg'; @State imageUri: string | undefined = undefined; @State imageSource: image.ImageSource | undefined = undefined; @State pixelMap: image.PixelMap | undefined = undefined; @@ -80,7 +83,7 @@ struct Index { try { let pickerProfile: cameraPicker.PickerProfile = { cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK }; - let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(getContext(this), + let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(this.UIContext.getHostContext(), [cameraPicker.PickerMediaType.PHOTO], pickerProfile); this.imageUri = pickerResult.resultUri ? pickerResult.resultUri : this.imageUri; hilog.info(0x0000, TAG, 'cameraPicker.pick succeed, uri: ' + JSON.stringify(pickerResult)); @@ -98,14 +101,14 @@ struct Index { try { let documentSelectOptions = new picker.DocumentSelectOptions(); documentSelectOptions.maxSelectNumber = 1; - let documentPicker = new picker.DocumentViewPicker(getContext(this)); + let documentPicker = new picker.DocumentViewPicker(this.context); documentPicker.select(documentSelectOptions).then((documentSelectResult: Array) => { hilog.info(0x0000, TAG, 'DocumentViewPicker.select succeed, uri: ' + JSON.stringify(documentSelectResult)); if (documentSelectResult[0].endsWith('.jpg') || documentSelectResult[0].endsWith('.png')) { this.imageUri = documentSelectResult[0] ? documentSelectResult[0] : this.imageUri; } else { - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.document_alert'), duration: 2000 }) @@ -179,7 +182,7 @@ struct Index { .height(40) .onClick(async () => { if (!this.pixelMap) { - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.no_pixel_map_alert'), duration: 2000 }); @@ -187,7 +190,7 @@ struct Index { } await pixelMap2File(this.pixelMap, this.path); - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.save_in_sandbox_success'), duration: 2000 }); @@ -199,7 +202,7 @@ struct Index { .height(40) .onClick(async (event, result: SaveButtonOnClickResult) => { if (!this.pixelMap) { - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.no_pixel_map_alert'), duration: 2000 }); @@ -209,12 +212,12 @@ struct Index { if (result === SaveButtonOnClickResult.SUCCESS) { try { await pixelMap2File(this.pixelMap, this.path); - let context = getContext(); + let context = this.UIContext.getHostContext(); let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(context, this.path); await phAccessHelper.applyChanges(assetChangeRequest); - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.save_in_gallery_success'), duration: 2000 }); @@ -258,7 +261,7 @@ struct Index { .height(40) .onClick(() => { if (!this.imageUri) { - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.no_image_alert'), duration: 2000 }) @@ -282,7 +285,7 @@ struct Index { }); }); - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.get_image_info_success'), duration: 2000 }) @@ -293,7 +296,7 @@ struct Index { .height(40) .onClick(() => { if (!this.imageSource) { - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.no_image_info_alert'), duration: 2000 }) @@ -302,7 +305,7 @@ struct Index { this.imageSource.createPixelMap().then((pixelMap: image.PixelMap) => { this.pixelMap = pixelMap; - promptAction.showToast({ + this.UIContext.getPromptAction().showToast({ message: $r('app.string.get_pixel_map_success'), duration: 2000 }) -- Gitee