js-apis-filemanager.md 11.0 KB
Newer Older
A
Annie_wang 已提交
1
# User File Access and Management
A
Annie_wang 已提交
2
>**NOTE**<br/>
A
annie_wangli 已提交
3
>
A
Annie_wang 已提交
4
>- The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
A
annie_wangli 已提交
5
>- The APIs of this module are system APIs and cannot be called by third-party applications. Currently, these APIs can be called only by **filepicker**.
A
Annie_wang 已提交
6

A
Annie_wang 已提交
7 8
This module provides service APIs for accessing and managing user files. It interworks with the underlying file management services to implement media library and external card management, and provides capabilities for applications to query and create user files.

A
annie_wangli 已提交
9 10 11
## Modules to Import

```js
A
annie_wangli 已提交
12
import filemanager from '@ohos.fileManager';
A
annie_wangli 已提交
13 14 15 16 17 18
```

## filemanager.getRoot

getRoot(options? : {dev? : DevInfo}) : Promise&lt;FileInfo[]&gt;

A
Annie_wang 已提交
19
Obtains information about the root album or directory in asynchronous mode. This API uses a promise to return the result.
A
annie_wangli 已提交
20

A
annie_wangli 已提交
21 22
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
23 24 25
- Parameters
  | Name| Type| Mandatory| Description|
  | --- | --- | --- | -- |
A
Annie_wang 已提交
26
  | options | Object | No| The options are as follows:<br>- &nbsp;**dev**: See [DevInfo](#devinfo). It is **dev = {name: "local"}** by default if not specified. Currently, only 'local' is supported.|
A
annie_wangli 已提交
27 28 29 30 31

- Return value

  | Type| Description|
  | --- | -- |
A
Annie_wang 已提交
32
  | Promise&lt;[FileInfo](#fileinfo)[]&gt; | Promise used to return the root album or directory information obtained.|
A
annie_wangli 已提交
33 34 35

- Example

A
Annie_wang 已提交
36 37 38 39 40 41 42 43 44 45 46
  ```js
  filemanager.getRoot().then((fileInfo) => {
      if(Array.isArray(fileInfo)) {
          for (var i = 0; i < fileInfo.length; i++) {
              console.log("file:"+JSON.stringify(fileInfo));
          }
      }
  }).catch((err) => {
      console.log(err)
  });
  ```
A
annie_wangli 已提交
47 48 49 50 51

## filemanager.getRoot

getRoot(options? : {dev? : DevInfo}, callback : AsyncCallback&lt;FileInfo[]&gt;) : void

A
Annie_wang 已提交
52
Obtains information about the root album or directory in asynchronous mode. This API uses a callback to return the result.
A
annie_wangli 已提交
53

A
annie_wangli 已提交
54 55
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
56 57
- Parameters

A
annie_wangli 已提交
58
  | Name  | Type                     | Mandatory| Description                         |
A
annie_wangli 已提交
59
  | -------- | ------------------------- | ---- | ----------------------------- |
A
Annie_wang 已提交
60 61
  | options | Object | No| The options are as follows:<br>- &nbsp;**dev**: See [DevInfo](#devinfo). It is **dev = {name: "local"}** by default if not specified. Currently, only 'local' is supported.|
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes  | Callback invoked to return the root album or directory information obtained. |
A
annie_wangli 已提交
62 63 64

- Example

A
Annie_wang 已提交
65 66 67 68 69 70 71 72 73
  ```js
  filemanager.getRoot((err, fileInfo) => {
      if(Array.isArray(fileInfo)) {
          for (var i = 0; i < fileInfo.length; i++) {
              console.log("file:"+JSON.stringify(fileInfo));
          }
      }
  });
  ```
A
annie_wangli 已提交
74 75 76 77 78

## filemanager.listFile

listFile(path : string, type : string, options? : {dev? : DevInfo, offset? : number, count? : number}) : Promise&lt;FileInfo[]&gt;

A
Annie_wang 已提交
79
Obtains information about the second-level album or files in asynchronous mode. This API uses a promise to return the result.
A
annie_wangli 已提交
80

A
annie_wangli 已提交
81 82
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
83 84 85
- Parameters
  | Name| Type| Mandatory| Description|
  | --- | --- | --- | -- |
A
Annie_wang 已提交
86 87
  | path | string | Yes| URI of the directory to query.|
  | type | string | Yes| Type of the files to query. The file type can be **file**, **image**, **audio**, or **video**.|
A
Annie_wang 已提交
88 89
  | options | Object | No| The options are as follows:<br>- &nbsp;**dev**: See [DevInfo](#devinfo). It is **dev = {name: "local"}** by default if not specified. Currently, only 'local' is supported.<br>- &nbsp;**offset**: position to start the query. The value is a number.<br>- &nbsp;**count**: number of files to query.|
  
A
annie_wangli 已提交
90 91 92 93
- Return value

  | Type| Description|
  | --- | -- |
A
Annie_wang 已提交
94
  | Promise&lt;FileInfo[]&gt; | Promise used to return the album or file information obtained.|
A
annie_wangli 已提交
95 96

- Error
A
annie_wangli 已提交
97
  | Error Info| Error Code|Description|
A
Annie_wang 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  | -- | --- | -- |
  | No such file or directory | 2      | The directory or album of the specified URI does not exist.|
  | No such process | 3 | Failed to obtain the FMS service.|
  | Not a directory | 20 | The object specified by the URI is not a directory or album.|

- Example

  ```js
  // Obtain all files in the directory.
  // Call listFile() and getRoot() to obtain the file URI.
  let media_path = file.uri
  filemanager.listFile(media_path, "file")
  .then((fileInfo) => {
      if(Array.isArray(fileInfo)) {
          for (var i = 0; i < fileInfo.length; i++) {
              console.log("file:"+JSON.stringify(fileInfo));
          }
      }
  }).catch((err) => {
      console.log(err)
  });
  ```
A
annie_wangli 已提交
120 121 122 123 124

## filemanager.listFile

listFile(path : string, type : string, options? : {dev? : DevInfo, offset? : number, count? : number}, callback : AsyncCallback&lt;FileInfo[]&gt;) : void

A
Annie_wang 已提交
125
Obtains information about the second-level album or files in asynchronous mode. This API uses a callback to return the result.
A
annie_wangli 已提交
126

A
annie_wangli 已提交
127 128
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
129 130
- Parameters

A
annie_wangli 已提交
131
  | Name  | Type                     | Mandatory| Description                                                        |
A
annie_wangli 已提交
132
  | -------- | ------------------------- | ---- | ------------------------------------------------------------ |
A
Annie_wang 已提交
133 134
  | path     | string                    | Yes  | URI of the directory to query.                                               |
  | type     | string                    | Yes  | Type of the files to query. The file type can be **file**, **image**, **audio**, or **video**.|
A
Annie_wang 已提交
135
  | options | Object | No| The options are as follows:<br>- &nbsp;**dev**: See [DevInfo](#devinfo). It is **dev = {name: "local"}** by default if not specified. Currently, only 'local' is supported.<br>- &nbsp;**offset**: position to start the query. The value is a number.<br>- &nbsp;**count**: number of files to query.|
A
annie_wangli 已提交
136
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes  | Callback invoked to return the file information obtained.                                |
A
annie_wangli 已提交
137 138
- Error

A
annie_wangli 已提交
139
  | Error Info                 | Error Code| Description                     |
A
annie_wangli 已提交
140
  | ------------------------- | ------ | ------------------------- |
A
Annie_wang 已提交
141 142 143
  | No such file or directory | 2      | The directory or album of the specified URI does not exist.|
  | No such process           | 3      | Failed to obtain the FMS service.          |
  | Not a directory           | 20     | The object specified by the URI is not a directory or album.|
A
annie_wangli 已提交
144

A
Annie_wang 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
- Example

  ```js
  // Call listFile() and getRoot() to obtain the file URI.
  let media_path = file.uri
  filemanager.listFile(media_path, "file", (err, fileInfo) => {
      if(Array.isArray(fileInfo)) {
          for (var i = 0; i < fileInfo.length; i++) {
              console.log("file:"+JSON.stringify(fileInfo));
          }
      }
  });
  ```
A
annie_wangli 已提交
158 159 160

## filemanager.createFile

A
Annie_wang 已提交
161
createFile(path : string, filename : string, options? : {dev? : DevInfo})  :   Promise&lt;string&gt;
A
annie_wangli 已提交
162

A
Annie_wang 已提交
163
Creates a file in the specified path in asynchronous mode. This API uses a promise to return the result.
A
annie_wangli 已提交
164

A
annie_wangli 已提交
165 166
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
167 168 169
- Parameters
  | Name| Type| Mandatory| Description|
  | --- | --- | --- | -- |
A
Annie_wang 已提交
170 171
  | filename | string | Yes| Name of the file to create.|
  | path | string | Yes| URI of the file to create.|
A
Annie_wang 已提交
172
  | options | Object | No| The options are as follows:<br>- &nbsp;**dev**: See [DevInfo](#devinfo). It is **dev = {name: "local"}** by default if not specified. Currently, only 'local' is supported.|
A
annie_wangli 已提交
173 174 175 176 177

- Return value

  | Type| Description|
  | --- | -- |
A
Annie_wang 已提交
178
  | Promise&lt;string&gt; | Promise used to return the URI of the file created.|
A
annie_wangli 已提交
179 180

- Error
A
annie_wangli 已提交
181 182 183
  | Error Info| Error Code|Description|
  | -- | --- | -- |
  | Operation not permitted | 1 | A file with the same name already exists.|
A
Annie_wang 已提交
184
  | No such file or directory | 2 | The directory or album of the specified URI does not exist.|
A
annie_wangli 已提交
185
  | No such process | 3 | Failed to obtain the FMS service.|
A
Annie_wang 已提交
186
  | Not a directory | 20 | The object specified by the URI is not a directory or album.|
A
annie_wangli 已提交
187

A
Annie_wang 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200
- Example

  ```js
  // Create a file.
  let media_path = file.uri // Obtain the file URI using listFile() and getRoot().
  let name = "xxx.jpg" // File to be saved.
  filemanager.createFile(media_path, name).then((uri) => {
      // The URI of the file created is returned.
      console.log("file uri:"+uri);
  }).catch((err) => {
      console.log(err);
  });
  ```
A
annie_wangli 已提交
201 202 203 204 205

## filemanager.createFile

createFile(path : string, filename: string, options? : {dev? : DevInfo}, callback : AsyncCallback&lt;string&gt;) : void

A
Annie_wang 已提交
206
Creates a file in the specified path in asynchronous mode. This API uses a callback to return the result.
A
annie_wangli 已提交
207

A
annie_wangli 已提交
208 209
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
210 211
- Parameters

A
annie_wangli 已提交
212
  | Name  | Type                     | Mandatory| Description                         |
A
annie_wangli 已提交
213
  | -------- | ------------------------- | ---- | ----------------------------- |
A
Annie_wang 已提交
214 215
  | filename | string                    | Yes  | Name of the file to create.               |
  | path     | string                    | Yes  | URI of the file to create.            |
A
Annie_wang 已提交
216
  | options | Object | No| The options are as follows:<br>- &nbsp;**dev**: See [DevInfo](#devinfo). It is **dev = {name: "local"}** by default if not specified. Currently, only 'local' is supported.|
A
annie_wangli 已提交
217
  | callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes  | Callback invoked to return the file information obtained. |
A
annie_wangli 已提交
218 219 220

- Error

A
annie_wangli 已提交
221
  | Error Info                 | Error Code| Description                     |
A
annie_wangli 已提交
222
  | ------------------------- | ------ | ------------------------- |
A
annie_wangli 已提交
223
  | Operation not permitted   | 1      | A file with the same name already exists.             |
A
Annie_wang 已提交
224
  | No such file or directory | 2      | The directory or album of the specified URI does not exist.|
A
annie_wangli 已提交
225
  | No such process           | 3      | Failed to obtain the FMS service.          |
A
Annie_wang 已提交
226
  | Not a directory           | 20     | The object specified by the URI is not a directory or album.|
A
annie_wangli 已提交
227

A
Annie_wang 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240
- Example

  ```js
  // Create a file.
  // Call listFile() and getRoot() to obtain the file URI.
  let media_path = file.uri
  // File to be saved.
  let name = "xxx.jpg"
  filemanager.createFile(media_path, name, (err, uri) => {
      // The URI of the file created is returned.
      console.log("file uri:"+uri);
  });
  ```
A
annie_wangli 已提交
241 242 243 244

## FileInfo
Defines the file information returned by **getRoot()** or **listFile()**.

A
annie_wangli 已提交
245 246
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
247 248 249 250
### Attributes

| Name| Type| Readable| Writable| Description|
| --- | -- | -- | -- | -- |
A
Annie_wang 已提交
251 252 253
| name | string | Yes| No| File name.|
| path | string | Yes| No| URI of the file.|
| type | string | Yes| No| File type.|
A
annie_wangli 已提交
254 255 256 257 258
| size | number | Yes| No| File size.|
| addedTime | number | Yes| No| Time when the file was scanned to the database.|
| modifiedTime | number | Yes| No| Time when the file was modified.|

## DevInfo
A
Annie_wang 已提交
259

A
annie_wangli 已提交
260 261
Defines the device type.

A
annie_wangli 已提交
262 263
**System capability**: SystemCapability.FileManagement.FileManagerService

A
annie_wangli 已提交
264 265
### Attributes

A
Annie_wang 已提交
266 267
| Name| Type  | Readable| Writable| Description    |
| ------ | ------ | ---- | ---- | -------- |
A
Annie_wang 已提交
268
| name   | string | Yes  | Yes  | Device name.|