From 244ab4890ccc7446e21643a2fb8c928b32d1e4e4 Mon Sep 17 00:00:00 2001 From: liujiahui Date: Wed, 7 May 2025 16:29:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E5=BC=83=E6=8E=A5=E5=8F=A3=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/entryability/EntryAbility.ets | 2 ++ .../main/ets/pages/BackgroundFileTransfer.ets | 7 ++++--- .../main/ets/pages/ResumableFileTransfer.ets | 4 +++- entry/src/main/ets/utils/CommonUtil.ets | 3 ++- entry/src/main/ets/utils/LocalFileUtil.ets | 19 +++++++++++++------ 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/entry/src/main/ets/entryability/EntryAbility.ets index 6d1c003..c622031 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -36,6 +36,8 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); + let uiContext: UIContext | undefined = windowStage.getMainWindowSync().getUIContext() + AppStorage.setOrCreate('uiContext', uiContext); }); } diff --git a/entry/src/main/ets/pages/BackgroundFileTransfer.ets b/entry/src/main/ets/pages/BackgroundFileTransfer.ets index 7d376f6..b323f6b 100644 --- a/entry/src/main/ets/pages/BackgroundFileTransfer.ets +++ b/entry/src/main/ets/pages/BackgroundFileTransfer.ets @@ -25,7 +25,8 @@ import { copyFileSync, getAlbumAsset, selectImagesFromAlbum } from '../utils/Loc import { ProgressBtn } from '../components/ProgressButton'; import { getProgressPercent, showErrorMessage, showSuccessMessage } from '../utils/CommonUtil'; import { Logger } from '../utils/Logger'; - +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +let context = uiContext!.getHostContext()!; @Component export struct BackgroundFileTransfer { @Consume navPageInfos: NavPathStack; @@ -110,12 +111,12 @@ export struct BackgroundFileTransfer { try { const wantAgentObj = await wantAgent.getWantAgent(this.getWantAgentInfo()); await backgroundTaskManager.startBackgroundRunning( - getContext(this), + context, backgroundTaskManager.BackgroundMode.DATA_TRANSFER, wantAgentObj ); task().finally(() => { - backgroundTaskManager.stopBackgroundRunning(getContext(this)); + backgroundTaskManager.stopBackgroundRunning(context); }); } catch (err) { Logger.error('Failed to start background task', JSON.stringify(err)); diff --git a/entry/src/main/ets/pages/ResumableFileTransfer.ets b/entry/src/main/ets/pages/ResumableFileTransfer.ets index 1a33312..4223313 100644 --- a/entry/src/main/ets/pages/ResumableFileTransfer.ets +++ b/entry/src/main/ets/pages/ResumableFileTransfer.ets @@ -15,7 +15,7 @@ import { rcp } from '@kit.RemoteCommunicationKit'; import { fileIo } from '@kit.CoreFileKit'; -import { promptAction } from '@kit.ArkUI'; + import { CustomListItem } from '../components/CustomList'; import { SelectionList } from '../components/SelectionList'; import { getFileList, ResumableDownloadManager } from '../service/FileRequest'; @@ -24,6 +24,8 @@ import { getAlbumAsset } from '../utils/LocalFileUtil'; import { ProgressBtn } from '../components/ProgressButton'; import { getProgressPercent, showSuccessMessage } from '../utils/CommonUtil'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +let promptAction = uiContext!.getPromptAction()!; @Component export struct ResumableFileTransfer { @Consume navPageInfos: NavPathStack; diff --git a/entry/src/main/ets/utils/CommonUtil.ets b/entry/src/main/ets/utils/CommonUtil.ets index 90437b9..36730f5 100644 --- a/entry/src/main/ets/utils/CommonUtil.ets +++ b/entry/src/main/ets/utils/CommonUtil.ets @@ -13,7 +13,8 @@ * limitations under the License. */ -import { promptAction } from '@kit.ArkUI'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +let promptAction = uiContext!.getPromptAction()!; export function getChunkRanges(totalSize: number, chunkSize: number): number[][] { const chunkCount = Math.ceil(totalSize / chunkSize); diff --git a/entry/src/main/ets/utils/LocalFileUtil.ets b/entry/src/main/ets/utils/LocalFileUtil.ets index af9323c..886f0c7 100644 --- a/entry/src/main/ets/utils/LocalFileUtil.ets +++ b/entry/src/main/ets/utils/LocalFileUtil.ets @@ -17,8 +17,11 @@ import { fileIo, fileUri } from '@kit.CoreFileKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { imageExtensions } from '../constants/Constants'; +const uiContext: UIContext | undefined = AppStorage.get('uiContext'); +let context = uiContext!.getHostContext()!; + export function getSandboxPath(path: string) { - return `${getContext().filesDir}/${path}`; + return `${context.filesDir}/${path}`; } export function copyFileSync(srcPath: string, destPath: string) { @@ -31,18 +34,18 @@ export function copyFileSync(srcPath: string, destPath: string) { function getPhotoType(fileNameExtension: string) { return imageExtensions.includes(fileNameExtension) ? - photoAccessHelper.PhotoType.IMAGE : - photoAccessHelper.PhotoType.VIDEO; + photoAccessHelper.PhotoType.IMAGE : + photoAccessHelper.PhotoType.VIDEO; } export async function getAlbumAsset(fileNameExtension: string) { - const phAccessHelper = photoAccessHelper.getPhotoAccessHelper(getContext()); + const phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); return await phAccessHelper.createAsset(getPhotoType(fileNameExtension), fileNameExtension); } // DocsCode 1 export async function saveImageToAlbum(sandboxPath: string) { - const phAccessHelper = photoAccessHelper.getPhotoAccessHelper(getContext()); + const phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); const fileNameExtension = sandboxPath.split('.').pop() || 'png'; const photoCreationConfig: photoAccessHelper.PhotoCreationConfig = { fileNameExtension, @@ -51,10 +54,13 @@ export async function saveImageToAlbum(sandboxPath: string) { const uri: string = fileUri.getUriFromPath(sandboxPath); const desFileUris: string[] = await phAccessHelper.showAssetsCreationDialog([uri], [photoCreationConfig]); const filePath = desFileUris[0]; - if (!filePath) throw new Error('photo assets permission denied'); + if (!filePath) { + throw new Error('photo assets permission denied'); + } copyFileSync(sandboxPath, filePath); return filePath; } + // DocsCode 1 // DocsCode 2 @@ -74,4 +80,5 @@ export async function selectImagesFromAlbum(maxNumber: number = 1): Promise