• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Developing a FileManager Application (Available Only for System Applications)
2
3OpenHarmony is prebuilt with the **FileManager** application. You can also develop your own **FileManager** as required.
4
5## Available APIs
6
7For details about the APIs, see [User File Access and Management](../reference/apis/js-apis-fileAccess.md).
8
9## How to Develop
10
111. Configure the permissions required and import dependent modules.
12   Apply for the **ohos.permission.FILE_ACCESS_MANAGER** and **ohos.permission.GET_BUNDLE_INFO_PRIVILEGED** permissions. For details, see [Applying for Permissions](../security/accesstoken-guidelines.md).
13
14   > **NOTE**
15   >
16   > **ohos.permission.FILE_ACCESS_MANAGER** is required for using the user file access framework APIs.
17   >
18   > **ohos.permission.GET_BUNDLE_INFO_PRIVILEGED** is required for querying information about file management server applications supported by the system.
19
202. Import the dependent modules.
21
22   ```ts
23   import fileAccess from '@ohos.file.fileAccess';
24   import fileExtensionInfo from '@ohos.file.fileExtensionInfo';
25   ```
26
27   The **fileAccess** module provides APIs for basic file operations, and the **fileExtensionInfo** module provides key structs for application development.
28
293. Query device information.
30   You can obtain attributes of one or all devices managed by the file management server in the current system. You can also filter devices as required.
31
32   In the user file access framework, **RootInfo** indicates the attribute information of a device. For example, obtain **RootInfo** of all devices.
33
34   ```ts
35   // Create a helper object for connecting to all file management servers in the system.
36   let fileAccessHelperAllServer = null;
37   createFileAccessHelper() {
38     try {    // this.context is the context passed from EntryAbility.
39       fileAccessHelperAllServer = fileAccess.createFileAccessHelper(this.context);
40       if (!fileAccessHelperAllServer) {
41         console.error("createFileAccessHelper interface returns an undefined object");
42       }
43     } catch (error) {
44         console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message);
45     }
46   }
47   async getRoots() {
48     let rootIterator = null;
49     let rootInfos = [];
50     let isDone = false;
51     try {
52       rootIterator = await fileAccessHelperAllServer.getRoots();
53       if (!rootIterator) {
54         console.error("getRoots interface returns an undefined object");
55         return;
56       }
57       while (!isDone) {
58         let result = rootIterator.next();
59         console.info("next result = " + JSON.stringify(result));
60         isDone = result.done;
61         if (!isDone)
62           rootinfos.push(result.value);
63       }
64     } catch (error) {
65       console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
66     }
67   }
68   ```
69
704. View directories.
71   In the user file access framework, **FileInfo** indicates basic information about a file (directory). You can use **listfile()** to traverse all files (directories) of the next level to obtain a **FileIterator** object or use **scanfile()** to filter the specified directories and obtain the **FileIterator** object that meets the conditions.
72
73    Currently, **listfile()** and **scanfile()** can be called by the **RootInfo** object to traverse lower-level files or filter the entire directory tree. In addition, **listfile()** and **scanfile()** can be called by the **FileInfo** object to traverse lower-level files or filter specified directories.
74
75   ```ts
76   // Start from the root directory.
77   let rootInfo = rootinfos[0];
78   let fileInfos = [];
79   let isDone = false;
80   let filter = {suffix: [".txt", ".jpg", ".xlsx"]}; // Set filter criteria.
81   try {
82     let fileIterator = rootInfo.listFile();          // Traverse the root directory of rootinfos[0] and return an iterator object.
83     // let fileIterator = rootInfo.scanFile(filter); // Filter the file information of device rootinfos[0] that meets the specified conditions and return an iteration object.
84     if (!fileIterator) {
85       console.error("listFile interface returns an undefined object");
86       return;
87     }
88     while (!isDone) {
89       let result = fileIterator.next();
90       console.info("next result = " + JSON.stringify(result));
91       isDone = result.done;
92       if (!isDone)
93         fileInfos.push(result.value);
94     }
95   } catch (error) {
96     console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
97   }
98
99   // Start from the specified directory.
100   let fileInfoDir = fileInfos[0]; // fileInfoDir indicates information about a directory.
101   let subFileInfos = [];
102   let isDone = false;
103   let filter = {suffix: [".txt", ".jpg", ".xlsx"]}; // Set filter criteria.
104   try {
105     let fileIterator = fileInfoDir.listFile(); // Traverse files in the specified directory and return an iterator object.
106     // let fileIterator = rootInfo.scanFile(filter); // Filter the files in the specified directory and return an iterator object.
107     if (!fileIterator) {
108       console.error("listFile interface returns an undefined object");
109       return;
110     }
111     while (!isDone) {
112       let result = fileIterator.next();
113       console.info("next result = " + JSON.stringify(result));
114       isDone = result.done;
115       if (!isDone)
116         subfileInfos.push(result.value);
117     }
118   } catch (error) {
119     console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
120   }
121   ```
122
1235. Perform operations on files or directories.
124   You can integrate APIs of the user file access framework to implement user behaviors, such as deleting, renaming, creating, and moving a file (directory). The following example shows how to create a file. For details about other APIs, see [User File Access and Management](../reference/apis/js-apis-fileAccess.md).
125
126   ```ts
127   // The local device is used as an example.
128   // Create a file.
129   // sourceUri is the URI in fileinfo of the Download directory.
130   // You need to use the obtained URI for development.
131   let sourceUri = "datashare:///media/file/6";
132   let displayName = "file1";
133   let fileUri = null;
134   try {
135     // Obtain fileAccessHelper by referring to the sample code of fileAccess.createFileAccessHelper.
136     fileUri = await fileAccessHelper.createFile(sourceUri, displayName);
137     if (!fileUri) {
138       console.error("createFile return undefined object");
139       return;
140     }
141     console.info("createFile sucess, fileUri: " + JSON.stringify(fileUri));
142   } catch (error) {
143     console.error("createFile failed, errCode:" + error.code + ", errMessage:" + error.message);
144   };
145   ```
146