js-apis-filemanager.md 11.5 KB
Newer Older
A
Annie_wang 已提交
1
# User File Access and Management
A
Annie_wang 已提交
2 3 4

The fileManager module provides 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_wang 已提交
5
>**NOTE**<br/>
A
annie_wangli 已提交
6
>
A
Annie_wang 已提交
7
>- 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 已提交
8
>- 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 已提交
9

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

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

## filemanager.getRoot

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

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

A
Annie_wang 已提交
22
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
23

A
Annie_wang 已提交
24 25 26 27
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| 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 已提交
28

A
Annie_wang 已提交
29
**Return value**
A
annie_wangli 已提交
30

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

A
Annie_wang 已提交
35
**Example**
A
annie_wangli 已提交
36

A
Annie_wang 已提交
37 38 39 40 41 42 43 44 45 46 47
  ```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 已提交
48 49 50 51 52

## filemanager.getRoot

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

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

A
Annie_wang 已提交
55
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
56

A
Annie_wang 已提交
57
**Parameters**
A
annie_wangli 已提交
58

A
Annie_wang 已提交
59 60 61 62
| Name  | Type                     | Mandatory| Description                         |
| -------- | ------------------------- | ---- | ----------------------------- |
| 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 已提交
63

A
Annie_wang 已提交
64
**Example**
A
annie_wangli 已提交
65

A
Annie_wang 已提交
66
  ```js
A
Annie_wang 已提交
67 68 69 70 71 72
  let options = {
    "dev":{
      "name":"local"
    }
  };
  filemanager.getRoot(options, (err, fileInfo)=>{
A
Annie_wang 已提交
73 74 75 76
      if(Array.isArray(fileInfo)) {
          for (var i = 0; i < fileInfo.length; i++) {
              console.log("file:"+JSON.stringify(fileInfo));
          }
A
Annie_wang 已提交
77
      } 
A
Annie_wang 已提交
78
  });
A
Annie_wang 已提交
79
  
A
Annie_wang 已提交
80
  ```
A
annie_wangli 已提交
81 82 83 84 85

## filemanager.listFile

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

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

A
Annie_wang 已提交
88
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
89

A
Annie_wang 已提交
90 91 92 93 94 95
**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| 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**.|
| 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 已提交
96

A
Annie_wang 已提交
97
**Return value**
A
annie_wangli 已提交
98

A
Annie_wang 已提交
99 100 101
| Type| Description|
| --- | -- |
| Promise&lt;FileInfo[]&gt; | Promise used to return the album or file information obtained.|
A
Annie_wang 已提交
102

A
Annie_wang 已提交
103 104 105 106 107 108 109 110 111
**Error**

| Error Info| Error Code|Description|
| -- | --- | -- |
| 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**
A
Annie_wang 已提交
112 113 114 115

  ```js
  // Obtain all files in the directory.
  // Call listFile() and getRoot() to obtain the file URI.
A
Annie_wang 已提交
116
  let media_path = ""
A
Annie_wang 已提交
117 118 119 120 121 122 123 124
  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) => {
A
Annie_wang 已提交
125
      console.log("Failed to get file"+err);
A
Annie_wang 已提交
126 127
  });
  ```
A
annie_wangli 已提交
128 129 130 131 132

## filemanager.listFile

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

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

A
Annie_wang 已提交
135
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
136

A
Annie_wang 已提交
137
**Parameters**
A
annie_wangli 已提交
138

A
Annie_wang 已提交
139 140 141 142 143 144
| Name  | Type                     | Mandatory| Description                                                        |
| -------- | ------------------------- | ---- | ------------------------------------------------------------ |
| 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**.|
| 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.|
| callback | AsyncCallback&lt;[FileInfo](#fileinfo)[]&gt; | Yes  | Callback invoked to return the root album or directory information obtained.                                |
A
annie_wangli 已提交
145

A
Annie_wang 已提交
146
**Error**
A
annie_wangli 已提交
147

A
Annie_wang 已提交
148 149 150 151 152 153 154
| Error Info                 | Error Code| Description                     |
| ------------------------- | ------ | ------------------------- |
| 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**
A
Annie_wang 已提交
155 156

  ```js
A
Annie_wang 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  // Call listFile() and getRoot() to obtain the file path.
  let fileInfos = filemanager.getRoot(); 
  let media_path  = "";
  for (let i = 0; i < fileInfos.length; i++) {
	if (fileInfos[i].name == "image_album") {
	  media_path = fileInfos[i].path;
	} else if (fileInfos[i].name == "audio_album") {
	  media_path = fileInfos[i].path;
	} else if (fileInfos[i].name == "video_album") {
	  media_path = fileInfos[i].path;
	} else if (fileInfos[i].name == "file_folder") {
	  media_path = fileInfos[i].path;
	}
  }

  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("Failed to get file"+err);
A
Annie_wang 已提交
181 182
  });
  ```
A
annie_wangli 已提交
183 184 185

## filemanager.createFile

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

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

A
Annie_wang 已提交
190 191 192 193 194 195 196 197
**System capability**: SystemCapability.FileManagement.UserFileService

**Parameters**
| Name| Type| Mandatory| Description|
| --- | --- | --- | -- |
| filename | string | Yes| Name of the file to create.|
| path | string | Yes| URI of the file to create.|
| 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 已提交
198

A
Annie_wang 已提交
199
**Return value**
A
annie_wangli 已提交
200

A
Annie_wang 已提交
201 202 203
| Type| Description|
| --- | -- |
| Promise&lt;string&gt; | Promise used to return the URI of the file created.|
A
annie_wangli 已提交
204

A
Annie_wang 已提交
205
**Error**
A
annie_wangli 已提交
206

A
Annie_wang 已提交
207 208 209 210 211 212
| Error Info| Error Code|Description|
| -- | --- | -- |
| Operation not permitted | 1 | A file with the same name already exists.|
| 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 已提交
213

A
Annie_wang 已提交
214
**Example**
A
Annie_wang 已提交
215 216 217

  ```js
  // Create a file.
A
Annie_wang 已提交
218
  let media_path = "" // Obtain the file URI using listFile() and getRoot().
A
Annie_wang 已提交
219 220 221 222 223 224 225 226
  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 已提交
227 228 229 230 231

## filemanager.createFile

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

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

A
Annie_wang 已提交
234
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
235

A
Annie_wang 已提交
236
**Parameters**
A
annie_wangli 已提交
237

A
Annie_wang 已提交
238 239 240 241 242 243
| Name  | Type                     | Mandatory| Description                         |
| -------- | ------------------------- | ---- | ----------------------------- |
| filename | string                    | Yes  | Name of the file to create.               |
| path     | string                    | Yes  | URI of the file to create.            |
| 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 已提交
244

A
Annie_wang 已提交
245
**Error**
A
annie_wangli 已提交
246

A
Annie_wang 已提交
247 248 249 250 251 252
| Error Info                 | Error Code| Description                     |
| ------------------------- | ------ | ------------------------- |
| Operation not permitted   | 1      | A file with the same name already exists.             |
| 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 已提交
253

A
Annie_wang 已提交
254
**Example**
A
Annie_wang 已提交
255 256 257 258

  ```js
  // Create a file.
  // Call listFile() and getRoot() to obtain the file URI.
A
Annie_wang 已提交
259
  let media_path = ""
A
Annie_wang 已提交
260 261
  // File to be saved.
  let name = "xxx.jpg"
A
Annie_wang 已提交
262 263 264 265 266 267 268 269
  let options = {
    "dev":{
      "name":"local"
    }
  };
  filemanager.createFile(media_path, name, options, function(err, uri) {
    // The URI of the file created is returned.
    console.log("file uri:"+uri);
A
Annie_wang 已提交
270
  });
A
Annie_wang 已提交
271

A
Annie_wang 已提交
272
  ```
A
annie_wangli 已提交
273 274 275 276

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

A
Annie_wang 已提交
277
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
278

A
annie_wangli 已提交
279 280 281 282
### Attributes

| Name| Type| Readable| Writable| Description|
| --- | -- | -- | -- | -- |
A
Annie_wang 已提交
283 284 285
| name | string | Yes| No| File name.|
| path | string | Yes| No| URI of the file.|
| type | string | Yes| No| File type.|
A
annie_wangli 已提交
286 287 288 289 290
| 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 已提交
291

A
annie_wangli 已提交
292 293
Defines the device type.

A
Annie_wang 已提交
294
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
295

A
annie_wangli 已提交
296 297
### Attributes

A
Annie_wang 已提交
298 299
| Name| Type  | Readable| Writable| Description    |
| ------ | ------ | ---- | ---- | -------- |
A
Annie_wang 已提交
300
| name   | string | Yes  | Yes  | Device name.|