js-apis-filemanager.md 11.5 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
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_wang 已提交
8

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_wang 已提交
21
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
22

A
Annie_wang 已提交
23 24 25 26
**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 已提交
27

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

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

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

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_wang 已提交
54
**System capability**: SystemCapability.FileManagement.UserFileService
A
annie_wangli 已提交
55

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

A
Annie_wang 已提交
58 59 60 61
| 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 已提交
62

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

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

## filemanager.listFile

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

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

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

A
Annie_wang 已提交
89 90 91 92 93 94
**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 已提交
95

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

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

A
Annie_wang 已提交
102 103 104 105 106 107 108 109 110
**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 已提交
111 112 113 114

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

## filemanager.listFile

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

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

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

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

A
Annie_wang 已提交
138 139 140 141 142 143
| 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 已提交
144

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

A
Annie_wang 已提交
147 148 149 150 151 152 153
| 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 已提交
154 155

  ```js
A
Annie_wang 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  // 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 已提交
180 181
  });
  ```
A
annie_wangli 已提交
182 183 184

## filemanager.createFile

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

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

A
Annie_wang 已提交
189 190 191 192 193 194 195 196
**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 已提交
197

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

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

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

A
Annie_wang 已提交
206 207 208 209 210 211
| 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 已提交
212

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

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

## filemanager.createFile

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

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

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

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

A
Annie_wang 已提交
237 238 239 240 241 242
| 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 已提交
243

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

A
Annie_wang 已提交
246 247 248 249 250 251
| 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 已提交
252

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

  ```js
  // Create a file.
  // Call listFile() and getRoot() to obtain the file URI.
A
Annie_wang 已提交
258
  let media_path = ""
A
Annie_wang 已提交
259 260
  // File to be saved.
  let name = "xxx.jpg"
A
Annie_wang 已提交
261 262 263 264 265 266 267 268
  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 已提交
269
  });
A
Annie_wang 已提交
270

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

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

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

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

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

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

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

A
annie_wangli 已提交
295 296
### Attributes

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