diff --git a/entry/src/main/ets/common/utils/ReadFile.ets b/entry/src/main/ets/common/utils/ReadFile.ets index 5bfa86eb7571a4c892c8dab8dfec78216602c1dc..6b23a109a13f7964f2ace606be248167f987ed34 100644 --- a/entry/src/main/ets/common/utils/ReadFile.ets +++ b/entry/src/main/ets/common/utils/ReadFile.ets @@ -16,6 +16,7 @@ import { fileIo } from '@kit.CoreFileKit'; import { common } from '@kit.AbilityKit'; import { buffer } from '@kit.ArkTS'; +import { hilog } from '@kit.PerformanceAnalysisKit'; const uiContext: UIContext | undefined = AppStorage.get('uiContext'); @@ -30,17 +31,22 @@ let res: string = ''; * @return string. */ export function readFile(): string { - let filePath = filesDir + '/test.txt'; - let stat = fileIo.statSync(filePath); - let size = stat.size; - let buf = new ArrayBuffer(size); - // Open a file stream based on the file path. - let fileStream = fileIo.createStreamSync(filePath, "r+"); - // File stream reading information - fileStream.readSync(buf); - // Converts the read information to the string type and returns the string type. - let con = buffer.from(buf, 0); - res = con.toString(); - fileStream.close(); - return res; + try { + let filePath = filesDir + '/test.txt'; + let stat = fileIo.statSync(filePath); + let size = stat.size; + let buf = new ArrayBuffer(size); + // Open a file stream based on the file path. + let fileStream = fileIo.createStreamSync(filePath, "r+"); + // File stream reading information + fileStream.readSync(buf); + // Converts the read information to the string type and returns the string type. + let con = buffer.from(buf, 0); + res = con.toString(); + fileStream.close(); + return res; + } catch (error) { + hilog.error(0x0000, 'readFile', `readFile catch error, code: ${error.code}, message: ${error.message}`); + return ''; + } } diff --git a/entry/src/main/ets/common/utils/WriteFile.ets b/entry/src/main/ets/common/utils/WriteFile.ets index b1cfd57e5f9f53638a0a87f71e16a809e8fb6007..ded9fd60ba3b8d52a046ac2bd212991cc69f7fc6 100644 --- a/entry/src/main/ets/common/utils/WriteFile.ets +++ b/entry/src/main/ets/common/utils/WriteFile.ets @@ -15,6 +15,7 @@ import { fileIo } from '@kit.CoreFileKit'; import { common } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; const uiContext: UIContext | undefined = AppStorage.get('uiContext'); let context = uiContext!.getHostContext() as common.UIAbilityContext; @@ -26,9 +27,13 @@ let filesDir = context.filesDir; * @param content Contents to be written to the file */ export function writeFile(content: string): void { - let filePath = filesDir + '/test.txt'; - // Open a file stream based on the file path. - let fileStream = fileIo.createStreamSync(filePath, "w+"); - fileStream.writeSync(content); - fileStream.close(); + try { + let filePath = filesDir + '/test.txt'; + // Open a file stream based on the file path. + let fileStream = fileIo.createStreamSync(filePath, "w+"); + fileStream.writeSync(content); + fileStream.close(); + } catch (error) { + hilog.error(0x0000, 'readFile', `readFile catch error, code: ${error.code}, message: ${error.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 9827b546ee821dd7c141a34e939b10a73e9cf803..f0a91e14a8e69555a988e6e6be82d3cdd32718c0 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/entry/src/main/ets/entryability/EntryAbility.ets @@ -36,7 +36,12 @@ export default class EntryAbility extends UIAbility { return; } hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); - AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); + + try { + AppStorage.setOrCreate('uiContext', windowStage.getMainWindowSync().getUIContext()); + } catch (error) { + hilog.error(0x0000, 'testTag', `getMainWindowSync catch error, code: ${error.code}, message: ${error.message}`); + } }); } diff --git a/entry/src/main/ets/view/PublicFilesTab.ets b/entry/src/main/ets/view/PublicFilesTab.ets index 5dfae59b01b3194a1afdf2692eb1095507d8e708..cc9747072d468fc8d15c373955c640b9b038ea27 100644 --- a/entry/src/main/ets/view/PublicFilesTab.ets +++ b/entry/src/main/ets/view/PublicFilesTab.ets @@ -49,7 +49,11 @@ export struct publicFilesTab { let context = this.getUIContext().getHostContext(); let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); // Creating a Media File - let uri = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg'); + let uri = await phAccessHelper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'jpg') + .catch((error: BusinessError) => { + Logger.error(`createAsset catch error, code: ${error.code}, message: ${error.message}`); + return ''; + }) Logger.info('createAsset successfully, uri: ' + uri); // Open the created media file and read the local file and convert it to ArrayBuffer for easy filling. let file = await fileIo.open(uri, fileIo.OpenMode.READ_WRITE);