dev-user-file-manager.md 7.3 KB
Newer Older
A
Annie_wang 已提交
1
# Developing a File Manager Application (for System Applications Only)
A
Annie_wang 已提交
2

A
Annie_wang 已提交
3
OpenHarmony is prebuilt with the **FileManager** application. You can also develop your own file manager application as required.
A
Annie_wang 已提交
4 5 6

## Available APIs

A
Annie_wang 已提交
7
For details about the APIs used to develop a file manager application, see [User File Access and Management](../reference/apis/js-apis-fileAccess.md).
A
Annie_wang 已提交
8 9 10

## How to Develop

A
Annie_wang 已提交
11
1. Apply for permissions required.
A
Annie_wang 已提交
12

A
Annie_wang 已提交
13
   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).
A
Annie_wang 已提交
14 15
   > **NOTE**
   >
A
Annie_wang 已提交
16 17
   > - **ohos.permission.FILE_ACCESS_MANAGER** allows your application to use the user file access framework APIs.
   >- **ohos.permission.GET_BUNDLE_INFO_PRIVILEGED** allows your application to obtain information about file management server applications supported by the system.
A
Annie_wang 已提交
18
2. Import dependent modules.
A
Annie_wang 已提交
19 20 21 22

   ```ts
   import fileAccess from '@ohos.file.fileAccess';
   import fileExtensionInfo from '@ohos.file.fileExtensionInfo';
A
Annie_wang 已提交
23 24 25
   import { Filter } from '@ohos.file.fs';
   import common from '@ohos.app.ability.common';
   import { BusinessError } from '@ohos.base';
A
Annie_wang 已提交
26 27 28 29
   ```

   The **fileAccess** module provides APIs for basic file operations, and the **fileExtensionInfo** module provides key structs for application development.

A
Annie_wang 已提交
30 31
3. Query device information.

A
Annie_wang 已提交
32
   You can obtain attributes of the devices managed by one or all file management servers in the system. You can also filter devices as required.
A
Annie_wang 已提交
33 34 35 36

   In the user file access framework, **RootInfo** indicates the attribute information of a device. For example, obtain **RootInfo** of all devices.

   ```ts
A
Annie_wang 已提交
37 38 39
   // Obtain the application context.
   let context = getContext(this) as common.UIAbilityContext;

A
Annie_wang 已提交
40
   // Create a helper object for connecting to all file management servers in the system.
A
Annie_wang 已提交
41 42
   let fileAccessHelperAllServer: fileAccess.FileAccessHelper;
   function createFileAccessHelper() {
A
Annie_wang 已提交
43
     try {    // this.context is the context passed from EntryAbility.
A
Annie_wang 已提交
44
       fileAccessHelperAllServer = fileAccess.createFileAccessHelper(context);
A
Annie_wang 已提交
45 46 47
       if (!fileAccessHelperAllServer) {
         console.error("createFileAccessHelper interface returns an undefined object");
       }
A
Annie_wang 已提交
48 49
     } catch (err) {
         let error: BusinessError = err as BusinessError;
A
Annie_wang 已提交
50 51 52
         console.error("createFileAccessHelper failed, errCode:" + error.code + ", errMessage:" + error.message);
     }
   }
A
Annie_wang 已提交
53 54 55 56
   let rootInfos: Array<fileAccess.RootInfo> = [];
   async function getRoots() {
     let rootIterator: fileAccess.RootIterator;
     let isDone: boolean = false;
A
Annie_wang 已提交
57 58 59 60
     try {
       rootIterator = await fileAccessHelperAllServer.getRoots();
       if (!rootIterator) {
         console.error("getRoots interface returns an undefined object");
A
Annie_wang 已提交
61 62
         return;
       }
A
Annie_wang 已提交
63 64 65 66 67
       while (!isDone) {
         let result = rootIterator.next();
         console.info("next result = " + JSON.stringify(result));
         isDone = result.done;
         if (!isDone)
A
Annie_wang 已提交
68 69 70 71
           rootInfos.push(result.value);
       }
     } catch (err) {
       let error: BusinessError = err as BusinessError;
A
Annie_wang 已提交
72 73 74 75 76 77
       console.error("getRoots failed, errCode:" + error.code + ", errMessage:" + error.message);
     }
   }
   ```

4. View directories.
A
Annie_wang 已提交
78

A
Annie_wang 已提交
79
   In the user file access framework, **FileInfo** indicates basic information about a file (directory). You can use **listfile()** to obtain a **FileIterator** object that traverses all files (directories) of the next level or use **scanfile()** to obtain a **FileIterator** object that meets the specified conditions.
A
Annie_wang 已提交
80

A
Annie_wang 已提交
81
   Currently, **listfile()** and **scanfile()** can be called by the **RootInfo** object to traverse the next-level files or filter the entire directory tree. In addition, **listfile()** and **scanfile()** can be called by the **FileInfo** object to traverse the next-level files or filter the specified directories.
A
Annie_wang 已提交
82 83 84

   ```ts
   // Start from the root directory.
A
Annie_wang 已提交
85 86 87 88 89 90 91
   let rootInfo = rootInfos[0];
   let fileInfos: Array<fileAccess.FileInfo> = [];
   let isDone: boolean = false;
   let filter: Filter = {suffix : [".txt", ".jpg", ".xlsx"]}; // Set the filter.
   try {
     let fileIterator = rootInfo.listFile();                  // Traverse the root directory of rootinfos[0] and return an iterator object.
     // let fileIterator = rootInfo.scanFile(filter);         // Filter device rootinfos[0] files that meet the specified conditions and return an iteration object.
A
Annie_wang 已提交
92 93 94 95 96 97 98 99 100 101
     if (!fileIterator) {
       console.error("listFile interface returns an undefined object");
     }
     while (!isDone) {
       let result = fileIterator.next();
       console.info("next result = " + JSON.stringify(result));
       isDone = result.done;
       if (!isDone)
         fileInfos.push(result.value);
     }
A
Annie_wang 已提交
102 103
   } catch (err) {
    let error: BusinessError = err as BusinessError;
A
Annie_wang 已提交
104 105
     console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
   }
A
Annie_wang 已提交
106

A
Annie_wang 已提交
107
   // Start from the specified directory.
A
Annie_wang 已提交
108 109 110 111
   let fileInfoDir = fileInfos[0]; // fileInfoDir indicates information about a directory.
   let subFileInfos: Array<fileAccess.FileInfo> = [];
   let isDone02: boolean = false;
   let filter02: Filter = {suffix : [".txt", ".jpg", ".xlsx"]}; // Set the filter.
A
Annie_wang 已提交
112
   try {
A
Annie_wang 已提交
113 114
     let fileIterator = fileInfoDir.listFile();                 // Traverse files in the specified directory and return an iterator object.
     // let fileIterator = rootInfo.scanFile(filter02);         // Filter the files in the specified directory and return an iterator object.
A
Annie_wang 已提交
115 116 117
     if (!fileIterator) {
       console.error("listFile interface returns an undefined object");
     }
A
Annie_wang 已提交
118
     while (!isDone02) {
A
Annie_wang 已提交
119 120
       let result = fileIterator.next();
       console.info("next result = " + JSON.stringify(result));
A
Annie_wang 已提交
121 122 123
       isDone02 = result.done;
       if (!isDone02)
         subFileInfos.push(result.value);
A
Annie_wang 已提交
124
     }
A
Annie_wang 已提交
125 126
   } catch (err) {
    let error: BusinessError = err as BusinessError;
A
Annie_wang 已提交
127 128 129 130 131
     console.error("listFile failed, errCode:" + error.code + ", errMessage:" + error.message);
   }
   ```

5. Perform operations on files or directories.
A
Annie_wang 已提交
132

A
Annie_wang 已提交
133 134 135 136 137 138 139
   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).

   ```ts
   // The local device is used as an example.
   // Create a file.
   // sourceUri is the URI in fileinfo of the Download directory.
   // You need to use the obtained URI for development.
A
Annie_wang 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
   async function creatFile() {
     let sourceUri: string = "file://docs/storage/Users/currentUser/Download";
     let displayName: string = "file1";
     let fileUri: string;
     try {
       // Obtain fileAccessHelperAllServer by referring to the sample code of fileAccess.createFileAccessHelper.
       fileUri = await fileAccessHelperAllServer.createFile(sourceUri, displayName);
       if (!fileUri) {
         console.error("createFile return undefined object");
       }
       console.info("createFile sucess, fileUri: " + JSON.stringify(fileUri));
     } catch (err) {
      let error: BusinessError = err as BusinessError;
      console.error("createFile failed, errCode:" + error.code + ", errMessage:" + error.message);
     };
   }
A
Annie_wang 已提交
156
   ```