• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# File Management Development
2
3## Does fileio.rmdir Delete Files Recursively?
4
5Applicable to: OpenHarmony SDK 3.2.6.3, stage model of API version 9
6
7Yes. **fileio.rmdir** deletes files recursively.
8
9## How Do I Create a File That Does Not Exist?
10
11Applicable to: OpenHarmony SDK 3.2.6.3, stage model of API version 9
12
13You can use **fileio.open(filePath, 0o100, 0o666)**. The second parameter **0o100** means to create a file if it does not exist. The third parameter **mode** must also be specified.
14
15## What If "call fail callback fail, code: 202, data: json arguments illegal" Is Displayed?
16
17Applicable to: OpenHarmony SDK 3.2.6.3, stage model of API version 9
18
19When the **fileio** module is used to copy files, the file path cannot start with "file:///".
20
21## How Do I Read Files Outside the App Sandbox?
22
23Applicable to: OpenHarmony SDK 3.2.6.5, stage model of API version 9
24
25If the input parameter of the **fileio** API is **path**, only the sandbox directory of the current app obtained from the context can be accessed. To access data in other directories such as the user data, images, and videos, open the file as the data owner and operate with the file descriptor (FD) returned.
26
27For example, to read or write a file in Media Library, perform the following steps:
28
291. Use **getFileAssets()** to obtain the **fileAsset** object.
30
312. Use **fileAsset.open()** to obtain the FD.
32
333. Use the obtained FD as the **fileIo** API parameter to read and write the file.
34
35## What If the File Contains Garbled Characters?
36
37Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
38
39Read the file content from the buffer, and decode the file content using **util.TextDecoder**.
40
41Example:
42
43```
44import util from '@ohos.util'
45async function readFile(path) {
46  let stream = fileio.createStreamSync(path, "r+");
47  let readOut = await stream.read(new ArrayBuffer(4096));
48  let textDecoder = new util.TextDecoder("utf-8", { ignoreBOM: true });
49  let buffer = new Uint8Array(readOut.buffer)
50  let readString = textDecoder.decode(buffer, { stream: false });
51  console.log ("[Demo] File content read: "+ readString);
52}
53```
54
55## What Should I Do If There Is No Return Value or Error Captured After getAlbums Is Called?
56
57Applicable to: OpenHarmony SDK 3.2.5.3, stage model of API version 9
58
59The **ohos.permission.READ_MEDIA** is required for using **getAlbums()**. In addition, this permission needs user authorization. For details, see [OpenHarmony Permission List](../security/permission-list.md).
60
611. Configure the required permission in the **module.json5** file.
62
63   ```
64   "requestPermissions": [
65     {
66       "name": "ohos.permission.READ_MEDIA"
67     }
68   ]
69   ```
70
712. Add the code for user authorization before the **MainAbility.ts -> onWindowStageCreate** page is loaded.
72
73   ```
74   import abilityAccessCtrl from '@ohos.abilityAccessCtrl.d.ts';
75
76   private requestPermissions() {
77   let permissionList: Array<string> = [
78     "ohos.permission.READ_MEDIA"
79   ];
80   let atManager = abilityAccessCtrl.createAtManager();
81   atManager.requestPermissionsFromUser(this.context, permissionList)
82     .then(data => {
83       console.info(`request permission data result = ${data.authResults}`)
84     })
85     .catch(err => {
86       console.error(`fail to request permission error:${err}`)
87     })
88   }
89   ```
90
91## What Do I Do If the App Crashes When FetchFileResult() Is Called Multiple Times?
92
93Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
94
95Each time after the **FetchFileResult** object is called, call **FetchFileResult.close()** to release and invalidate the **FetchFileResult** object .
96
97## What If An Error Is Reported by IDE When mediaLibrary.getMediaLibrary() Is Called in the Stage Model?
98
99Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
100
101In the stage model, use **mediaLibrary.getMediaLibrary(context: Context)** to obtain the media library instance.
102
103## How Do I Sort the Data Returned by mediaLibrary.getFileAssets()?
104
105Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
106
107Use the **order** attribute in **[MediaFetchOptions](../reference/apis/js-apis-medialibrary.md#mediafetchoptions7)** to sort the data returned.
108