js-apis-medialibrary.md 99.1 KB
Newer Older
G
gloria 已提交
1
# @ohos.multimedia.medialibrary (Media Library Management)
W
wusongqing 已提交
2

W
wusongqing 已提交
3
> **NOTE**
G
Gloria 已提交
4
>
G
Gloria 已提交
5 6 7 8
> - The APIs of this module are supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version.
> - This API is deprecated since API version 9 and will be retained until API version 13.
> - Certain functionalities are changed as system APIs and can be used only by system applications. To use these functionalities, call [@ohos.filemanagement.userFileManager](js-apis-userFileManager.md).
> - The functionalities for selecting and storing media assets are still open to common applications. To use these functionalities, call [@ohos.file.picker](js-apis-file-picker.md).
W
wusongqing 已提交
9 10

## Modules to Import
11
```js
W
wusongqing 已提交
12
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
W
wusongqing 已提交
13 14
```

W
wusongqing 已提交
15
## mediaLibrary.getMediaLibrary<sup>8+</sup>
W
wusongqing 已提交
16 17 18

getMediaLibrary(context: Context): MediaLibrary

W
wusongqing 已提交
19
Obtains a **MediaLibrary** instance, which is used to access and modify personal media data such as audios, videos, images, and documents.
W
wusongqing 已提交
20

21 22
This API can be used only in the stage model.

W
wusongqing 已提交
23 24 25 26
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
27 28 29
| Name | Type   | Mandatory| Description                      |
| ------- | ------- | ---- | -------------------------- |
| context | Context | Yes  | Context of the ability.|
W
wusongqing 已提交
30 31 32

**Return value**

W
wusongqing 已提交
33 34
| Type                           | Description   |
| ----------------------------- | :---- |
W
wusongqing 已提交
35 36
| [MediaLibrary](#medialibrary) | **MediaLibrary** instance.|

W
wusongqing 已提交
37 38
**Example (from API version 9)**

39
```ts
G
Gloria 已提交
40
// Obtain a MediaLibrary instance. The instance obtained here is used in later.
41 42
const context = getContext(this);
let media = mediaLibrary.getMediaLibrary(context);
W
wusongqing 已提交
43 44 45
```

**Example (API version 8)**
W
wusongqing 已提交
46

47
```js
W
wusongqing 已提交
48 49
import featureAbility from '@ohos.ability.featureAbility';

50 51
let context = featureAbility.getContext();
let media = mediaLibrary.getMediaLibrary(context);
W
wusongqing 已提交
52
```
53

W
wusongqing 已提交
54 55 56 57 58 59
## mediaLibrary.getMediaLibrary

getMediaLibrary(): MediaLibrary

Obtains a **MediaLibrary** instance, which is used to access and modify personal media data such as audios, videos, images, and documents.

60 61
This API can be used only in the FA model.

W
wusongqing 已提交
62 63 64 65 66 67 68 69 70 71 72
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

| Type                         | Description      |
| ----------------------------- | :--------- |
| [MediaLibrary](#medialibrary) | **MediaLibrary** instance.|

**Example**

```js
73
let media = mediaLibrary.getMediaLibrary();
W
wusongqing 已提交
74 75
```

W
wusongqing 已提交
76 77
## MediaLibrary

W
wusongqing 已提交
78
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
79 80 81 82 83 84 85 86 87 88 89 90


getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void 

Obtains file assets (also called files). This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
91 92 93 94
| Name  | Type                                               | Mandatory| Description                             |
| -------- | --------------------------------------------------- | ---- | --------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)            | Yes  | Options for fetching the files.                     |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes  | Asynchronous callback of **FetchFileResult**.|
W
wusongqing 已提交
95 96 97

**Example**

98
```js
G
Gloria 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111
async function example() {
    let fileKeyObj = mediaLibrary.FileKey;
    let imageType = mediaLibrary.MediaType.IMAGE;
    // Create options for fetching the files. The options are used to obtain files of the image type.
    let imagesFetchOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
    };
    // Obtain the files in asynchronous callback mode.
    media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
        // Check whether the result set of the obtained files is undefined. If yes, the API call fails.
        if (fetchFileResult == undefined) {
            console.error('get fetchFileResult failed with error: ' + error);
112 113
            return;
        }
G
Gloria 已提交
114 115 116 117 118 119
        // Obtain the total number of files in the result set.
        const count = fetchFileResult.getCount();
        // Check whether the number is less than 0. If yes, the API call fails.
        if (count < 0) {
            console.error('get count from fetchFileResult failed, count: ' + count);
            return;
120
        }
G
Gloria 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        // Check whether the number is 0. If yes, the API call is successful, but the result set is empty. Check whether the options for fetching the files are correctly set and whether the corresponding files exist on the device.
        if (count == 0) {
            console.info('The count of fetchFileResult is zero');
            return;
        }
        console.info('Get fetchFileResult successfully, count: ' + count);
        // Obtain the first file in the result set in asynchronous callback mode.
        fetchFileResult.getFirstObject((error, fileAsset) => {
            // Check whether the first file is undefined. If yes, the API call fails.
            if (fileAsset == undefined) {
                console.error('get first object failed with error: ' + error);
                return;
            }
            console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
            // Call getNextObject to obtain the next file until the last one.
            for (let i = 1; i < count; i++) {
G
Gloria 已提交
137 138
                let fileAsset = await fetchFileResult.getNextObject();
                console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
G
Gloria 已提交
139
            }
G
Gloria 已提交
140 141
            // Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called.
            fetchFileResult.close();
G
Gloria 已提交
142
        });
143
    });
G
Gloria 已提交
144
}
W
wusongqing 已提交
145
```
G
Gloria 已提交
146

W
wusongqing 已提交
147
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
148 149 150 151 152 153 154 155 156 157 158

getFileAssets(options: MediaFetchOptions): Promise&lt;FetchFileResult&gt;

Obtains file assets. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
159 160 161
| Name | Type                                    | Mandatory| Description        |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | Yes  | Options for fetching the files.|
W
wusongqing 已提交
162 163 164

**Return value**

W
wusongqing 已提交
165 166 167
| Type                                | Description          |
| ------------------------------------ | -------------- |
| [FetchFileResult](#fetchfileresult7) | Result set of the file retrieval operation.|
W
wusongqing 已提交
168 169 170

**Example**

171
```js
G
Gloria 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
async function example() {
    let fileKeyObj = mediaLibrary.FileKey;
    let imageType = mediaLibrary.MediaType.IMAGE;
    // Create options for fetching the files. The options are used to obtain files of the image type.
    let imagesFetchOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
    };
    // Obtain the files in promise mode.
    media.getFileAssets(imagesFetchOp).then((fetchFileResult) => {
        // Obtain the total number of files in the result set.
        const count = fetchFileResult.getCount();
        // Check whether the number is less than 0. If yes, the API call fails.
        if (count < 0) {
            console.error('get count from fetchFileResult failed, count: ' + count);
            return;
188
        }
G
Gloria 已提交
189 190 191 192 193 194 195 196 197 198 199
        // Check whether the number is 0. If yes, the API call is successful, but the result set is empty. Check whether the options for fetching the files are correctly set and whether the corresponding files exist on the device.
        if (count == 0) {
            console.info('The count of fetchFileResult is zero');
            return;
        }
        console.info('Get fetchFileResult successfully, count: ' + count);
        // Obtain the first file in the result set in promise mode.
        fetchFileResult.getFirstObject().then((fileAsset) => {
            console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName);
            // Call getNextObject to obtain the next file until the last one.
            for (let i = 1; i < count; i++) {
G
Gloria 已提交
200 201
                let fileAsset = await fetchFileResult.getNextObject();
                console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName);
G
Gloria 已提交
202
            }
G
Gloria 已提交
203 204
            // Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called.
            fetchFileResult.close();
G
Gloria 已提交
205 206 207 208 209 210 211
        }).catch((error) => {
            // Calling getFirstObject fails.
            console.error('get first object failed with error: ' + error);
        });
    }).catch((error) => {
        // Calling getFileAssets fails.
        console.error('get file assets failed with error: ' + error);
212
    });
G
Gloria 已提交
213
}
W
wusongqing 已提交
214 215 216 217
```

### on<sup>8+</sup>

G
gloria 已提交
218
on(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback: Callback&lt;void&gt;): void
W
wusongqing 已提交
219 220 221 222 223 224 225

Subscribes to the media library changes. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
226 227
| Name     | Type                  | Mandatory  | Description                                      |
| -------- | -------------------- | ---- | ---------------------------------------- |
228
| type     | 'deviceChange'&#124;<br>'albumChange'&#124;<br>'imageChange'&#124;<br>'audioChange'&#124;<br>'videoChange'&#124;<br>'fileChange'&#124;<br>'remoteFileChange'               | Yes   | Media type.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device|
229
| callback | Callback&lt;void&gt; | Yes   | Void callback.                                   |
W
wusongqing 已提交
230 231 232

**Example**

233
```js
W
wusongqing 已提交
234
media.on('imageChange', () => {
W
wusongqing 已提交
235 236 237 238 239
    // image file had changed, do something
})
```
### off<sup>8+</sup>

G
gloria 已提交
240
off(type: 'deviceChange'&#124;'albumChange'&#124;'imageChange'&#124;'audioChange'&#124;'videoChange'&#124;'fileChange'&#124;'remoteFileChange', callback?: Callback&lt;void&gt;): void
W
wusongqing 已提交
241 242 243 244 245 246 247

Unsubscribes from the media library changes. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
248 249
| Name     | Type                  | Mandatory  | Description                                      |
| -------- | -------------------- | ---- | ---------------------------------------- |
250
| type     | 'deviceChange'&#124;<br>'albumChange'&#124;<br>'imageChange'&#124;<br>'audioChange'&#124;<br>'videoChange'&#124;<br>'fileChange'&#124;<br>'remoteFileChange'               | Yes   | Media type.<br>'deviceChange': registered device change<br>'albumChange': album change<br>'imageChange': image file change<br>'audioChange': audio file change<br>'videoChange': video file change<br>'fileChange': file change<br>'remoteFileChange': file change on the registered device|
251
| callback | Callback&lt;void&gt; | No   | Void callback.                                   |
W
wusongqing 已提交
252 253 254

**Example**

255
```js
W
wusongqing 已提交
256
media.off('imageChange', () => {
G
Gloria 已提交
257
    // Stop listening successfully.
W
wusongqing 已提交
258 259 260
})
```

261
### createAsset<sup>8+</sup>
W
wusongqing 已提交
262 263 264 265 266 267 268 269 270 271 272

createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback&lt;FileAsset&gt;): void

Creates a media asset. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
273 274 275 276 277 278
| Name      | Type                                   | Mandatory| Description                                                        |
| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8)                | Yes  | Media type.                                                    |
| displayName  | string                                  | Yes  | Display file name.                                                  |
| relativePath | string                                  | Yes  | Path for storing the file. You can use [getPublicDirectory](#getpublicdirectory8) to obtain the paths for storing different types of files.|
| callback     | AsyncCallback<[FileAsset](#fileasset7)> | Yes  | Asynchronous callback for **FileAsset**.                         |
W
wusongqing 已提交
279 280 281

**Example**

282
```js
W
wusongqing 已提交
283 284 285 286 287
async function example() {
    // Create an image file in callback mode.
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
G
Gloria 已提交
288
    media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (error, fileAsset) => {
W
wusongqing 已提交
289
        if (fileAsset != undefined) {
G
Gloria 已提交
290
            console.info('createAsset successfully, message');
W
wusongqing 已提交
291
        } else {
G
Gloria 已提交
292
            console.error('createAsset failed with error: ' + error);
W
wusongqing 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        }
    });
}
```

### createAsset<sup>8+</sup>

createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise&lt;FileAsset&gt;

Creates a media asset. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
310 311 312 313 314
| Name      | Type                    | Mandatory| Description                                                        |
| ------------ | ------------------------ | ---- | ------------------------------------------------------------ |
| mediaType    | [MediaType](#mediatype8) | Yes  | Media type.                                                    |
| displayName  | string                   | Yes  | Display file name.                                                  |
| relativePath | string                   | Yes  | Relative path. You can use [getPublicDirectory](#getpublicdirectory8) to obtain the relative path of the level-1 directory of different types of media files.|
W
wusongqing 已提交
315 316 317

**Return value**

W
wusongqing 已提交
318 319 320
| Type                    | Description             |
| ------------------------ | ----------------- |
| [FileAsset](#fileasset7) | Media data (FileAsset).|
W
wusongqing 已提交
321 322 323

**Example**

324
```js
325 326 327 328 329 330 331
async function example() {
    // Create an image file in promise mode.
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
    media.createAsset(mediaType, 'imagePromise.jpg', path + 'myPicture/').then((fileAsset) => {
        console.info('createAsset successfully, message = ' + JSON.stringify(fileAsset));
G
Gloria 已提交
332 333
    }).catch((error) => {
        console.error('createAsset failed with error: ' + error);
334 335
    });
}
W
wusongqing 已提交
336 337
```

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
### deleteAsset<sup>8+</sup>

deleteAsset(uri: string): Promise\<void>

Deletes a file asset. This API uses a promise to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

| Name     | Type                          | Mandatory  | Description             |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | Yes   | URI of the file asset to delete.|

**Return value**
| Type                 | Description                  |
| ------------------- | -------------------- |
| Promise&lt;void&gt; | Promise used to return the result.|

**Example**

```js
async function example() {
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
G
Gloria 已提交
374 375
        console.error('asset not exist');
        return;
376 377
    }
    media.deleteAsset(asset.uri).then(() => {
G
Gloria 已提交
378 379 380
        console.info('deleteAsset successfully');
    }).catch((error) => {
        console.error('deleteAsset failed with error: ' + error);
381
    });
G
Gloria 已提交
382
    fetchFileResult.close();
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
}
```

### deleteAsset<sup>8+</sup>
deleteAsset(uri: string, callback: AsyncCallback\<void>): void

Deletes a file asset. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

| Name     | Type                          | Mandatory  | Description             |
| -------- | ---------------------------- | ---- | --------------- |
| uri | string | Yes   | URI of the file asset to delete.|
|callback |AsyncCallback\<void>| Yes |Callback used to return the result.|

**Example**

```js
async function example() {
    let fileKeyObj = mediaLibrary.FileKey;
    let fileType = mediaLibrary.MediaType.FILE;
    let option = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
    };
    const fetchFileResult = await media.getFileAssets(option);
    let asset = await fetchFileResult.getFirstObject();
    if (asset == undefined) {
G
Gloria 已提交
417 418
        console.error('asset not exist');
        return;
419
    }
G
Gloria 已提交
420 421 422
    media.deleteAsset(asset.uri, (error) => {
        if (error != undefined) {
            console.error('deleteAsset failed with error: ' + error);
423
        } else {
G
Gloria 已提交
424
            console.info('deleteAsset successfully');
425 426
        }
    });
G
Gloria 已提交
427
    fetchFileResult.close();
428 429 430
}
```

W
wusongqing 已提交
431 432 433 434 435 436 437 438 439 440
### getPublicDirectory<sup>8+</sup>

getPublicDirectory(type: DirectoryType, callback: AsyncCallback&lt;string&gt;): void

Obtains a public directory. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
441 442 443 444
| Name  | Type                            | Mandatory| Description                     |
| -------- | -------------------------------- | ---- | ------------------------- |
| type     | [DirectoryType](#directorytype8) | Yes  | Type of the public directory.             |
| callback | AsyncCallback&lt;string&gt;      | Yes  | Callback used to return the public directory.|
W
wusongqing 已提交
445 446 447

**Example**

448
```js
W
wusongqing 已提交
449
let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
G
Gloria 已提交
450
media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => {
W
wusongqing 已提交
451
    if (dicResult == 'Camera/') {
G
Gloria 已提交
452
        console.info('getPublicDirectory DIR_CAMERA successfully');
W
wusongqing 已提交
453
    } else {
G
Gloria 已提交
454
        console.error('getPublicDirectory DIR_CAMERA failed with error: ' + error);
W
wusongqing 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468
    }
});
```

### getPublicDirectory<sup>8+</sup>

getPublicDirectory(type: DirectoryType): Promise&lt;string&gt;

Obtains a public directory. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
469 470 471
| Name| Type                            | Mandatory| Description        |
| ------ | -------------------------------- | ---- | ------------ |
| type   | [DirectoryType](#directorytype8) | Yes  | Type of the public directory.|
W
wusongqing 已提交
472 473 474

**Return value**

W
wusongqing 已提交
475 476 477
| Type            | Description            |
| ---------------- | ---------------- |
| Promise\<string> | Promise used to return the public directory.|
W
wusongqing 已提交
478 479 480

**Example**

481
```js
W
wusongqing 已提交
482 483
async function example() {
    let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA;
G
Gloria 已提交
484 485 486 487 488 489 490 491 492
    media.getPublicDirectory(DIR_CAMERA).then((dicResult) => {
        if (dicResult == 'Camera/') {
            console.info('getPublicDirectory DIR_CAMERA successfully');
        } else {
            console.error('getPublicDirectory DIR_CAMERA failed');
        }
    }).catch((error) => {
        console.error('getPublicDirectory failed with error: ' + error);
    });
W
wusongqing 已提交
493 494 495
}
```

W
wusongqing 已提交
496
### getAlbums<sup>7+</sup>
W
wusongqing 已提交
497

G
Gloria 已提交
498
getAlbums(options: MediaFetchOptions, callback: AsyncCallback&lt;Array&lt;Album&gt;&gt;): void
W
wusongqing 已提交
499 500 501 502 503 504 505 506 507

Obtains the albums. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
508 509 510 511
| Name  | Type                                        | Mandatory| Description                       |
| -------- | -------------------------------------------- | ---- | --------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)     | Yes  | Options for fetching the albums.               |
| callback | AsyncCallback&lt;Array<[Album](#album7)>&gt; | Yes  | Callback used to return the albums.|
W
wusongqing 已提交
512 513 514

**Example**

515
```js
G
Gloria 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => {
        if (albumList != undefined) {
            console.info('getAlbums successfully: ' + JSON.stringify(albumList));
        } else {
            console.error('getAlbums failed with error: ' + error);
        }
    })
}
W
wusongqing 已提交
529 530
```

W
wusongqing 已提交
531
### getAlbums<sup>7+</sup>
W
wusongqing 已提交
532

G
Gloria 已提交
533
getAlbums(options: MediaFetchOptions): Promise&lt;Array&lt;Album&gt;&gt;
W
wusongqing 已提交
534 535 536 537 538 539 540 541 542

Obtains the albums. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
543 544 545
| Name | Type                                    | Mandatory| Description        |
| ------- | ---------------------------------------- | ---- | ------------ |
| options | [MediaFetchOptions](#mediafetchoptions7) | Yes  | Options for fetching the albums.|
W
wusongqing 已提交
546 547 548

**Return value**

W
wusongqing 已提交
549 550 551
| Type                            | Description         |
| -------------------------------- | ------------- |
| Promise<Array<[Album](#album7)>> | Promise used to return the albums.|
W
wusongqing 已提交
552 553 554

**Example**

555
```js
G
Gloria 已提交
556 557 558 559 560 561 562 563 564 565 566
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => {
        console.info('getAlbums successfully: ' + JSON.stringify(albumList));
    }).catch((error) => {
        console.error('getAlbums failed with error: ' + error);
    });
}
W
wusongqing 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579
```

### release<sup>8+</sup>

release(callback: AsyncCallback&lt;void&gt;): void

Releases this **MediaLibrary** instance.
Call this API when you no longer need to use the APIs in the **MediaLibrary** instance.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
580 581
| Name     | Type                       | Mandatory  | Description        |
| -------- | ------------------------- | ---- | ---------- |
G
Gloria 已提交
582
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback that returns no value.|
W
wusongqing 已提交
583 584 585

**Example**

586
```js
G
Gloria 已提交
587
media.release(() => {
W
wusongqing 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    // do something
});
```

### release<sup>8+</sup>

release(): Promise&lt;void&gt;

Releases this **MediaLibrary** instance.
Call this API when you no longer need to use the APIs in the **MediaLibrary** instance.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
603 604
| Type                 | Description                  |
| ------------------- | -------------------- |
W
wusongqing 已提交
605 606 607 608
| Promise&lt;void&gt; | Promise used to return the execution result.|

**Example**

609
```js
W
wusongqing 已提交
610 611 612
media.release()
```

G
Gloria 已提交
613
### storeMediaAsset
W
wusongqing 已提交
614 615 616 617 618

storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback&lt;string&gt;): void

Stores a media asset. This API uses an asynchronous callback to return the URI that stores the media asset.

W
wusongqing 已提交
619 620
> **NOTE**
>
G
Gloria 已提交
621
> This API is supported since API version 6 and can be used only by the FA model.
W
wusongqing 已提交
622 623 624 625 626

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
627 628 629 630
| Name     | Type                                   | Mandatory  | Description                     |
| -------- | ------------------------------------- | ---- | ----------------------- |
| option   | [MediaAssetOption](#mediaassetoption) | Yes   | Media asset option.                |
| callback | AsyncCallback&lt;string&gt;           | Yes   | Callback used to return the URI that stores the media asset.|
W
wusongqing 已提交
631 632 633

**Example**

634
```js
W
wusongqing 已提交
635
let option = {
G
Gloria 已提交
636 637 638
    src : '/data/storage/el2/base/haps/entry/image.png',
    mimeType : 'image/*',
    relativePath : 'Pictures/'
W
wusongqing 已提交
639
};
G
Gloria 已提交
640 641 642
mediaLibrary.getMediaLibrary().storeMediaAsset(option, (error, value) => {
    if (error) {
        console.error('storeMediaAsset failed with error: ' + error);
W
wusongqing 已提交
643 644
        return;
    }
G
Gloria 已提交
645
    console.info('Media resources stored. ');
W
wusongqing 已提交
646 647
    // Obtain the URI that stores the media asset.
});
648
```
W
wusongqing 已提交
649 650


G
Gloria 已提交
651
### storeMediaAsset
W
wusongqing 已提交
652 653 654 655 656

storeMediaAsset(option: MediaAssetOption): Promise&lt;string&gt;

Stores a media asset. This API uses a promise to return the URI that stores the media asset.

W
wusongqing 已提交
657 658
> **NOTE**
>
G
Gloria 已提交
659
> This API is supported since API version 6 and can be used only by the FA model.
W
wusongqing 已提交
660 661 662 663 664

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
665 666 667
| Name   | Type                                   | Mandatory  | Description     |
| ------ | ------------------------------------- | ---- | ------- |
| option | [MediaAssetOption](#mediaassetoption) | Yes   | Media asset option.|
W
wusongqing 已提交
668 669 670

**Return value**

W
wusongqing 已提交
671 672
| Type                   | Description                          |
| --------------------- | ---------------------------- |
W
wusongqing 已提交
673 674 675 676
| Promise&lt;string&gt; | Promise used to return the URI that stores the media asset.|

**Example**

677
```js
W
wusongqing 已提交
678
let option = {
G
Gloria 已提交
679 680 681
    src : '/data/storage/el2/base/haps/entry/image.png',
    mimeType : 'image/*',
    relativePath : 'Pictures/'
W
wusongqing 已提交
682 683
};
mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => {
G
Gloria 已提交
684
    console.info('Media resources stored.');
W
wusongqing 已提交
685
    // Obtain the URI that stores the media asset.
G
Gloria 已提交
686 687
}).catch((error) => {
    console.error('storeMediaAsset failed with error: ' + error);
W
wusongqing 已提交
688
});
689
```
W
wusongqing 已提交
690 691


G
Gloria 已提交
692
### startImagePreview
W
wusongqing 已提交
693 694 695

startImagePreview(images: Array&lt;string&gt;, index: number, callback: AsyncCallback&lt;void&gt;): void

696
Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result.
W
wusongqing 已提交
697

W
wusongqing 已提交
698
> **NOTE**
G
Gloria 已提交
699 700
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
W
wusongqing 已提交
701 702 703 704 705

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
706 707
| Name     | Type                       | Mandatory  | Description                                      |
| -------- | ------------------------- | ---- | ---------------------------------------- |
G
Gloria 已提交
708
| images   | Array&lt;string&gt;       | Yes   | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.|
W
wusongqing 已提交
709 710
| index    | number                    | Yes   | Index of the first image to preview.                              |
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback used to return the image preview result. If the preview fails, an error message is returned.                       |
W
wusongqing 已提交
711 712 713

**Example**

714
```js
W
wusongqing 已提交
715
let images = [
G
Gloria 已提交
716 717
    'datashare:///media/xxxx/2',
    'datashare:///media/xxxx/3'
W
wusongqing 已提交
718
];
W
wusongqing 已提交
719
/* Preview online images.
W
wusongqing 已提交
720
let images = [
G
Gloria 已提交
721 722
    'https://media.xxxx.com/image1.jpg',
    'https://media.xxxx.com/image2.jpg'
W
wusongqing 已提交
723
];
W
wusongqing 已提交
724
*/
W
wusongqing 已提交
725
let index = 1;
G
Gloria 已提交
726 727 728
mediaLibrary.getMediaLibrary().startImagePreview(images, index, (error) => {
    if (error) {
        console.error('startImagePreview failed with error: ' + error);
W
wusongqing 已提交
729 730
        return;
    }
G
Gloria 已提交
731
    console.info('Succeeded in previewing the images.');
W
wusongqing 已提交
732
});
733
```
W
wusongqing 已提交
734 735


G
Gloria 已提交
736
### startImagePreview
W
wusongqing 已提交
737 738 739

startImagePreview(images: Array&lt;string&gt;, callback: AsyncCallback&lt;void&gt;): void

740
Starts image preview. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses an asynchronous callback to return the execution result.
W
wusongqing 已提交
741

W
wusongqing 已提交
742
> **NOTE**
G
Gloria 已提交
743 744
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
W
wusongqing 已提交
745 746 747 748 749

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
750 751
| Name     | Type                       | Mandatory  | Description                                      |
| -------- | ------------------------- | ---- | ---------------------------------------- |
G
Gloria 已提交
752
| images   | Array&lt;string&gt;       | Yes   | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.|
W
wusongqing 已提交
753
| callback | AsyncCallback&lt;void&gt; | Yes   | Callback used to return the image preview result. If the preview fails, an error message is returned.                       |
W
wusongqing 已提交
754 755 756

**Example**

757
```js
W
wusongqing 已提交
758
let images = [
G
Gloria 已提交
759 760
    'datashare:///media/xxxx/2',
    'datashare:///media/xxxx/3'
W
wusongqing 已提交
761
];
W
wusongqing 已提交
762
/* Preview online images.
W
wusongqing 已提交
763
let images = [
G
Gloria 已提交
764 765
    'https://media.xxxx.com/image1.jpg',
    'https://media.xxxx.com/image2.jpg'
W
wusongqing 已提交
766
];
W
wusongqing 已提交
767
*/
G
Gloria 已提交
768 769 770
mediaLibrary.getMediaLibrary().startImagePreview(images, (error) => {
    if (error) {
        console.error('startImagePreview failed with error: ' + error);
W
wusongqing 已提交
771 772
        return;
    }
G
Gloria 已提交
773
    console.info('Succeeded in previewing the images.');
W
wusongqing 已提交
774
});
775
```
W
wusongqing 已提交
776 777


G
Gloria 已提交
778
### startImagePreview
W
wusongqing 已提交
779 780 781

startImagePreview(images: Array&lt;string&gt;, index?: number): Promise&lt;void&gt;

782
Starts image preview, with the first image to preview specified. This API can be used to preview local images whose URIs start with **datashare://** or online images whose URIs start with **https://**. It uses a promise to return the execution result.
W
wusongqing 已提交
783

W
wusongqing 已提交
784
> **NOTE**
G
Gloria 已提交
785 786
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the **\<[Image](../arkui-ts/ts-basic-components-image.md)>** component instead. The **\<Image>** component can be used to render and display local and online images.
W
wusongqing 已提交
787 788 789 790 791

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
792 793
| Name   | Type                 | Mandatory  | Description                                      |
| ------ | ------------------- | ---- | ---------------------------------------- |
G
Gloria 已提交
794
| images | Array&lt;string&gt; | Yes   | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.|
W
wusongqing 已提交
795
| index  | number              | No   | Index of the first image to preview. If this parameter is not specified, the default value **0** is used.                     |
W
wusongqing 已提交
796 797 798

**Return value**

W
wusongqing 已提交
799 800
| Type                 | Description                             |
| ------------------- | ------------------------------- |
W
wusongqing 已提交
801 802 803 804
| Promise&lt;void&gt; | Promise used to return the image preview result. If the preview fails, an error message is returned.|

**Example**

805
```js
W
wusongqing 已提交
806
let images = [
G
Gloria 已提交
807 808
    'datashare:///media/xxxx/2',
    'datashare:///media/xxxx/3'
W
wusongqing 已提交
809
];
W
wusongqing 已提交
810
/* Preview online images.
W
wusongqing 已提交
811
let images = [
G
Gloria 已提交
812 813
    'https://media.xxxx.com/image1.jpg',
    'https://media.xxxx.com/image2.jpg'
W
wusongqing 已提交
814
];
W
wusongqing 已提交
815
*/
W
wusongqing 已提交
816 817
let index = 1;
mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => {
G
Gloria 已提交
818 819 820
    console.info('Succeeded in previewing the images.');
}).catch((error) => {
    console.error('startImagePreview failed with error: ' + error);
W
wusongqing 已提交
821
});
822
```
W
wusongqing 已提交
823 824


G
Gloria 已提交
825
### startMediaSelect
W
wusongqing 已提交
826 827 828 829 830

startMediaSelect(option: MediaSelectOption, callback: AsyncCallback&lt;Array&lt;string&gt;&gt;): void

Starts media selection. This API uses an asynchronous callback to return the list of URIs that store the selected media assets.

W
wusongqing 已提交
831
> **NOTE**
G
Gloria 已提交
832 833
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
W
wusongqing 已提交
834 835 836 837 838

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
839 840
| Name     | Type                                      | Mandatory  | Description                                  |
| -------- | ---------------------------------------- | ---- | ------------------------------------ |
G
Gloria 已提交
841
| option   | [MediaSelectOption](#mediaselectoption)  | Yes   | Media selection option.                             |
842
| callback | AsyncCallback&lt;Array&lt;string&gt;&gt; | Yes   | Callback used to return the list of URIs (starting with **datashare://**) that store the selected media assets.|
W
wusongqing 已提交
843 844 845

**Example**

846 847
```js
let option : mediaLibrary.MediaSelectOption = {
G
Gloria 已提交
848
    type : 'media',
W
wusongqing 已提交
849 850
    count : 2
};
G
Gloria 已提交
851 852 853
mediaLibrary.getMediaLibrary().startMediaSelect(option, (error, value) => {
    if (error) {
        console.error('startMediaSelect failed with error: ' + error);
W
wusongqing 已提交
854 855
        return;
    }
G
Gloria 已提交
856
    console.info('Media resources selected.');
W
wusongqing 已提交
857 858
    // Obtain the media selection value.
});
859
```
W
wusongqing 已提交
860 861


G
Gloria 已提交
862
### startMediaSelect
W
wusongqing 已提交
863 864 865 866 867

startMediaSelect(option: MediaSelectOption): Promise&lt;Array&lt;string&gt;&gt;

Starts media selection. This API uses a promise to return the list of URIs that store the selected media assets.

W
wusongqing 已提交
868
> **NOTE**
G
Gloria 已提交
869 870
> This API is supported since API version 6 and can be used only by the FA model.
> You are advised to use the system app Gallery instead. Gallery is a built-in visual resource access application that provides features such as image and video management and browsing. For details about how to use Gallery, visit [OpenHarmony/applications_photos](https://gitee.com/openharmony/applications_photos).
W
wusongqing 已提交
871 872 873 874 875

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
876 877
| Name   | Type                                     | Mandatory  | Description     |
| ------ | --------------------------------------- | ---- | ------- |
G
Gloria 已提交
878
| option | [MediaSelectOption](#mediaselectoption) | Yes   | Media selection option.|
W
wusongqing 已提交
879 880 881

**Return value**

W
wusongqing 已提交
882 883
| Type                                | Description                                      |
| ---------------------------------- | ---------------------------------------- |
884
| Promise&lt;Array&lt;string&gt;&gt; | Promise used to return the list of URIs (starting with **datashare://**) that store the selected media assets.|
W
wusongqing 已提交
885 886 887

**Example**

888 889
```js
let option : mediaLibrary.MediaSelectOption = {
G
Gloria 已提交
890
    type : 'media',
W
wusongqing 已提交
891 892 893
    count : 2
};
mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => {
G
Gloria 已提交
894
    console.info('Media resources selected.');
W
wusongqing 已提交
895
    // Obtain the media selection value.
G
Gloria 已提交
896 897
}).catch((error) => {
    console.error('startMediaSelect failed with error: ' + error);
W
wusongqing 已提交
898 899
});

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
```
### getActivePeers<sup>8+</sup>

getActivePeers(): Promise\<Array\<PeerInfo>>;

Obtains information about online peer devices. This API uses a promise to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore

**Return value**

| Type                 | Description                  |
| ------------------- | -------------------- |
G
gloria 已提交
917
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return the online peer devices, in an array of **PeerInfo** objects.|
918 919 920 921 922 923 924

**Example**

```js
async function example() {
    media.getActivePeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
G
Gloria 已提交
925
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
926
        } else {
G
Gloria 已提交
927
            console.info('get distributed info is undefined!');
928
        }
G
Gloria 已提交
929 930
    }).catch((error) => {
        console.error('get distributed info failed with error: ' + error);
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
    });
}
```

### getActivePeers<sup>8+</sup>

getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

Obtains information about online peer devices. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore

**Return value**

| Type                 | Description                  |
| ------------------- | -------------------- |
G
gloria 已提交
951
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return the online peer devices, in an array of **PeerInfo** objects.|
952 953 954 955 956

**Example**

```js
async function example() {
G
Gloria 已提交
957
    media.getActivePeers((error, devicesInfo) => {
958
        if (devicesInfo != undefined) {
G
Gloria 已提交
959
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
960
        } else {
G
Gloria 已提交
961
            console.error('get distributed failed with error: ' + error);
962
        }
G
Gloria 已提交
963
    });
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
}
```


### getAllPeers<sup>8+</sup>

getAllPeers(): Promise\<Array\<PeerInfo>>;

Obtains information about all peer devices. This API uses a promise to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore

**Return value**

| Type                 | Description                  |
| ------------------- | -------------------- |
G
gloria 已提交
984
|  Promise\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return all peer devices, in an array of **PeerInfo** objects.|
985 986 987 988 989 990 991

**Example**

```js
async function example() {
    media.getAllPeers().then((devicesInfo) => {
        if (devicesInfo != undefined) {
G
Gloria 已提交
992
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
993
        } else {
G
Gloria 已提交
994
            console.info('get distributed info is undefined!');
995
        }
G
Gloria 已提交
996 997
    }).catch((error) => {
        console.error('get distributed info failed with error: ' + error);
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
    });
}
```

### getAllPeers<sup>8+</sup>

getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void;

Obtains information about online peer devices. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore

**Return value**

| Type                 | Description                  |
| ------------------- | -------------------- |
G
gloria 已提交
1018
| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return all peer devices, in an array of **PeerInfo** objects.|
1019 1020 1021 1022 1023

**Example**

```js
async function example() {
G
Gloria 已提交
1024
    media.getAllPeers((error, devicesInfo) => {
1025
        if (devicesInfo != undefined) {
G
Gloria 已提交
1026
            console.info('get distributed info ' + JSON.stringify(devicesInfo));
1027
        } else {
G
Gloria 已提交
1028
            console.error('get distributed failed with error: ' + error);
1029
        }
G
Gloria 已提交
1030
    });
1031 1032
}
```
W
wusongqing 已提交
1033

W
wusongqing 已提交
1034
## FileAsset<sup>7+</sup>
W
wusongqing 已提交
1035 1036 1037

Provides APIs for encapsulating file asset attributes.

G
gloria 已提交
1038 1039 1040 1041
> **NOTE**
> 1. The system attempts to parse the file content if the file is an audio or video file. The actual field values will be restored from the passed values during scanning on some devices.
> 2. Some devices may not support the modification of **orientation**. You are advised to use [ModifyImageProperty](js-apis-image.md#modifyimageproperty9) of the **image** module.

W
wusongqing 已提交
1042 1043 1044 1045
### Attributes

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
1046
| Name                     | Type                    | Readable| Writable| Description                                                  |
W
wusongqing 已提交
1047
| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ |
W
wusongqing 已提交
1048
| id                        | number                   | Yes  | No  | File asset ID.                                          |
1049
| uri                       | string                   | Yes  | No  | File asset URI, for example, **datashare:///media/image/2**.        |
W
wusongqing 已提交
1050 1051 1052
| mimeType                  | string                   | Yes  | No  | Extended file attributes.                                          |
| mediaType<sup>8+</sup>    | [MediaType](#mediatype8) | Yes  | No  | Media type.                                              |
| displayName               | string                   | Yes  | Yes  | Display file name, including the file name extension.                                |
G
gloria 已提交
1053
| title                     | string                   | Yes  | Yes  | Title in the file. By default, it carries the file name without extension.                                              |
W
wusongqing 已提交
1054 1055 1056
| relativePath<sup>8+</sup> | string                   | Yes  | Yes  | Relative public directory of the file.                                      |
| parent<sup>8+</sup>       | number                   | Yes  | No  | Parent directory ID.                                              |
| size                      | number                   | Yes  | No  | File size, in bytes.                                |
G
Gloria 已提交
1057 1058 1059
| dateAdded                 | number                   | Yes  | No  | Date when the file was added. The value is the number of seconds elapsed since the Epoch time.        |
| dateModified              | number                   | Yes  | No  | Date when the file content (not the file name) was last modified. The value is the number of seconds elapsed since the Epoch time.|
| dateTaken                 | number                   | Yes  | No  | Date when the file (photo) was taken. The value is the number of seconds elapsed since the Epoch time.        |
W
wusongqing 已提交
1060 1061 1062 1063 1064
| artist<sup>8+</sup>       | string                   | Yes  | No  | Artist of the file.                                                  |
| audioAlbum<sup>8+</sup>   | string                   | Yes  | No  | Audio album.                                                  |
| width                     | number                   | Yes  | No  | Image width, in pixels.                                |
| height                    | number                   | Yes  | No  | Image height, in pixels.                                |
| orientation               | number                   | Yes  | Yes  | Image display direction (clockwise rotation angle, for example, 0, 90, or 180, in degrees).|
W
wusongqing 已提交
1065
| duration<sup>8+</sup>     | number                   | Yes  | No  | Duration, in ms.                                  |
W
wusongqing 已提交
1066 1067 1068
| albumId                   | number                   | Yes  | No  | ID of the album to which the file belongs.                                  |
| albumUri<sup>8+</sup>     | string                   | Yes  | No  | URI of the album to which the file belongs.                                     |
| albumName                 | string                   | Yes  | No  | Name of the album to which the file belongs.                                    |
W
wusongqing 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082


### isDirectory<sup>8+</sup>

isDirectory(callback: AsyncCallback&lt;boolean&gt;): void

Checks whether this file asset is a directory. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1083
| Name     | Type                          | Mandatory  | Description                 |
W
wusongqing 已提交
1084
| -------- | ---------------------------- | ---- | ------------------- |
W
wusongqing 已提交
1085
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to return whether the file asset is a directory.|
W
wusongqing 已提交
1086 1087 1088

**Example**

1089
```js
W
wusongqing 已提交
1090
async function example() {
G
Gloria 已提交
1091
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1092 1093
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1094 1095 1096
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1097 1098 1099
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1100 1101 1102 1103 1104 1105
    asset.isDirectory((error, isDirectory) => {
        if (error) {
            console.error('isDirectory failed with error: ' + error);
        } else {
            console.info('isDirectory result:' + isDirectory);
        }
W
wusongqing 已提交
1106
    });
G
Gloria 已提交
1107
    fetchFileResult.close();
W
wusongqing 已提交
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
}
```

### isDirectory<sup>8+</sup>

isDirectory():Promise&lt;boolean&gt;

Checks whether this file asset is a directory. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1123 1124
| Type                    | Description                          |
| ---------------------- | ---------------------------- |
W
wusongqing 已提交
1125 1126 1127 1128
| Promise&lt;boolean&gt; | Promise used to return whether the file asset is a directory.|

**Example**

1129
```js
W
wusongqing 已提交
1130
async function example() {
G
Gloria 已提交
1131
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1132 1133
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1134 1135 1136
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1137 1138 1139
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1140 1141 1142 1143
    asset.isDirectory().then((isDirectory) => {
        console.info('isDirectory result:' + isDirectory);
    }).catch((error) => {
        console.error('isDirectory failed with error: ' + error);
W
wusongqing 已提交
1144
    });
G
Gloria 已提交
1145
    fetchFileResult.close();
W
wusongqing 已提交
1146 1147 1148 1149 1150 1151 1152
}
```

### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void

W
wusongqing 已提交
1153
Commits the modification in this file asset to the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
1154 1155 1156 1157 1158 1159 1160

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1161 1162 1163
| Name     | Type                       | Mandatory  | Description   |
| -------- | ------------------------- | ---- | ----- |
| callback | AsyncCallback&lt;void&gt; | Yes   | Void callback.|
W
wusongqing 已提交
1164 1165 1166

**Example**

1167
```js
W
wusongqing 已提交
1168
async function example() {
G
Gloria 已提交
1169
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1170 1171
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1172 1173 1174
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1175 1176 1177 1178 1179
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.title = 'newtitle';
    asset.commitModify(() => {
G
Gloria 已提交
1180
        console.info('commitModify successfully');   
W
wusongqing 已提交
1181
    });
G
Gloria 已提交
1182
    fetchFileResult.close();
W
wusongqing 已提交
1183 1184 1185 1186 1187 1188 1189
}
```

### commitModify<sup>8+</sup>

commitModify(): Promise&lt;void&gt;

W
wusongqing 已提交
1190
Commits the modification in this file asset to the database. This API uses a promise to return the result.
W
wusongqing 已提交
1191 1192 1193 1194 1195 1196 1197

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1198 1199
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
1200 1201 1202 1203
| Promise&lt;void&gt; | Void promise.|

**Example**

1204
```js
W
wusongqing 已提交
1205
async function example() {
G
Gloria 已提交
1206
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1207 1208
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1209 1210 1211
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1212 1213 1214 1215
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
    asset.title = 'newtitle';
G
Gloria 已提交
1216 1217
    await asset.commitModify();
    fetchFileResult.close();
W
wusongqing 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226
}
```

### open<sup>8+</sup>

open(mode: string, callback: AsyncCallback&lt;number&gt;): void

Opens this file asset. This API uses an asynchronous callback to return the result.

G
Gloria 已提交
1227
**NOTE**: When a file is opened in 'w' mode, the returned FD cannot be read. However, due to the implementation differences of file systems, some user-mode files opened in 'w' mode can be read by using FD. To perform the read or write operation on a file by using FD, you are advised to open the file in 'rw' mode. The write operations are mutually exclusive. After a write operation is complete, you must call **close** to release the resource.
W
wusongqing 已提交
1228

W
wusongqing 已提交
1229
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
1230 1231 1232 1233 1234

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1235 1236
| Name     | Type                         | Mandatory  | Description                                 |
| -------- | --------------------------- | ---- | ----------------------------------- |
W
wusongqing 已提交
1237
| mode     | string                      | Yes   | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).|
G
Gloria 已提交
1238
| callback | AsyncCallback&lt;number&gt; | Yes   | Callback used to return the file descriptor.                           |
W
wusongqing 已提交
1239 1240 1241

**Example**

1242
```js
W
wusongqing 已提交
1243 1244 1245 1246
async function example() {
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
G
Gloria 已提交
1247 1248 1249 1250 1251 1252 1253
    const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
    asset.open('rw', (error, fd) => {
        if (fd > 0) {
            asset.close(fd);
        } else {
            console.error('File Open failed with error: ' + error);
        }
W
wusongqing 已提交
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    });
}
```

### open<sup>8+</sup>

open(mode: string): Promise&lt;number&gt;

Opens this file asset. This API uses a promise to return the result.

G
Gloria 已提交
1264
**NOTE**: When a file is opened in 'w' mode, the returned FD cannot be read. However, due to the implementation differences of file systems, some user-mode files opened in 'w' mode can be read by using FD. To perform the read or write operation on a file by using FD, you are advised to open the file in 'rw' mode. The write operations are mutually exclusive. After a write operation is complete, you must call **close** to release the resource.
W
wusongqing 已提交
1265

W
wusongqing 已提交
1266
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
1267 1268 1269 1270 1271

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1272 1273
| Name | Type    | Mandatory  | Description                                 |
| ---- | ------ | ---- | ----------------------------------- |
W
wusongqing 已提交
1274
| mode | string | Yes   | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).|
W
wusongqing 已提交
1275 1276 1277

**Return value**

W
wusongqing 已提交
1278 1279
| Type                   | Description           |
| --------------------- | ------------- |
G
Gloria 已提交
1280
| Promise&lt;number&gt; | Promise used to return the file descriptor.|
W
wusongqing 已提交
1281 1282 1283

**Example**

1284
```js
W
wusongqing 已提交
1285 1286 1287 1288
async function example() {
    let mediaType = mediaLibrary.MediaType.IMAGE;
    let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE;
    const path = await media.getPublicDirectory(DIR_IMAGE);
G
Gloria 已提交
1289 1290 1291 1292 1293 1294
    const asset = await media.createAsset(mediaType, 'image00003.jpg', path);
    asset.open('rw').then((fd) => {
        console.info('File open fd: ' + fd);
    }).catch((error) => {
        console.error('File open failed with error: ' + error);
    });
W
wusongqing 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303
}
```

### close<sup>8+</sup>

close(fd: number, callback: AsyncCallback&lt;void&gt;): void

Closes this file asset. This API uses an asynchronous callback to return the result.

W
wusongqing 已提交
1304
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
1305 1306 1307 1308 1309

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1310 1311 1312 1313
| Name     | Type                       | Mandatory  | Description   |
| -------- | ------------------------- | ---- | ----- |
| fd       | number                    | Yes   | File descriptor.|
| callback | AsyncCallback&lt;void&gt; | Yes   | Void callback.|
W
wusongqing 已提交
1314 1315 1316

**Example**

1317
```js
W
wusongqing 已提交
1318
async function example() {
G
Gloria 已提交
1319
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1320 1321
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1322 1323 1324
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1325 1326 1327
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
W
wusongqing 已提交
1328
    asset.open('rw').then((fd) => {
G
Gloria 已提交
1329 1330 1331 1332
        console.info('File open fd: ' + fd);
        asset.close(fd, (error) => {
            if (error) {
                console.error('asset.close failed with error: ' + error);
W
wusongqing 已提交
1333
            } else {
G
Gloria 已提交
1334
                console.info('asset.close successfully');
W
wusongqing 已提交
1335 1336
            }
        });
G
Gloria 已提交
1337 1338
    }).catch((error) => {
        console.error('File open failed with error: ' + error);
W
wusongqing 已提交
1339
    });
G
Gloria 已提交
1340
    fetchFileResult.close();
W
wusongqing 已提交
1341 1342 1343 1344 1345 1346 1347 1348 1349
}
```

### close<sup>8+</sup>

close(fd: number): Promise&lt;void&gt;

Closes this file asset. This API uses a promise to return the result.

W
wusongqing 已提交
1350
**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA
W
wusongqing 已提交
1351 1352 1353 1354 1355

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1356 1357 1358
| Name | Type    | Mandatory  | Description   |
| ---- | ------ | ---- | ----- |
| fd   | number | Yes   | File descriptor.|
W
wusongqing 已提交
1359 1360 1361

**Return value**

W
wusongqing 已提交
1362 1363
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
1364 1365 1366 1367
| Promise&lt;void&gt; | Void promise.|

**Example**

1368
```js
W
wusongqing 已提交
1369
async function example() {
G
Gloria 已提交
1370
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1371 1372
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1373 1374 1375
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1376 1377 1378
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
W
wusongqing 已提交
1379 1380
    asset.open('rw').then((fd) => {
        console.info('File fd!' + fd);
G
Gloria 已提交
1381 1382 1383 1384
        asset.close(fd).then(() => {
            console.info('asset.close successfully');
        }).catch((closeErr) => {
            console.error('asset.close fail, closeErr: ' + closeErr);
W
wusongqing 已提交
1385
        });
G
Gloria 已提交
1386 1387
    }).catch((error) => {
        console.error('open File failed with error: ' + error);
W
wusongqing 已提交
1388
    });
G
Gloria 已提交
1389
    fetchFileResult.close();
W
wusongqing 已提交
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
}
```

### getThumbnail<sup>8+</sup>

getThumbnail(callback: AsyncCallback&lt;image.PixelMap&gt;): void

Obtains the thumbnail of this file asset. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1405 1406 1407
| Name     | Type                                 | Mandatory  | Description              |
| -------- | ----------------------------------- | ---- | ---------------- |
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes   | Callback used to return the pixel map of the thumbnail.|
W
wusongqing 已提交
1408 1409 1410

**Example**

1411
```js
W
wusongqing 已提交
1412
async function example() {
G
Gloria 已提交
1413
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1414 1415
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1416 1417 1418
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1419 1420 1421
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1422 1423 1424 1425 1426 1427
    asset.getThumbnail((error, pixelmap) => {
        if (error) {
            console.error('mediaLibrary getThumbnail failed with error: ' + error);
        } else {
            console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
        }
W
wusongqing 已提交
1428
    });
G
Gloria 已提交
1429
    fetchFileResult.close();
W
wusongqing 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
}
```

### getThumbnail<sup>8+</sup>

getThumbnail(size: Size, callback: AsyncCallback&lt;image.PixelMap&gt;): void

Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1445 1446 1447 1448
| Name     | Type                                 | Mandatory  | Description              |
| -------- | ----------------------------------- | ---- | ---------------- |
| size     | [Size](#size8)                      | Yes   | Size of the thumbnail.           |
| callback | AsyncCallback&lt;image.PixelMap&gt; | Yes   | Callback used to return the pixel map of the thumbnail.|
W
wusongqing 已提交
1449 1450 1451

**Example**

1452
```js
W
wusongqing 已提交
1453
async function example() {
1454
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1455 1456
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1457 1458 1459
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1460
    };
W
wusongqing 已提交
1461
    let size = { width: 720, height: 720 };
W
wusongqing 已提交
1462 1463
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1464 1465 1466 1467 1468 1469
    asset.getThumbnail(size, (error, pixelmap) => {
        if (error) {
            console.error('mediaLibrary getThumbnail failed with error: ' + error);
        } else {
            console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
        }
W
wusongqing 已提交
1470
    });
G
Gloria 已提交
1471
    fetchFileResult.close();
W
wusongqing 已提交
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
}
```

### getThumbnail<sup>8+</sup>

getThumbnail(size?: Size): Promise&lt;image.PixelMap&gt;

Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1487 1488 1489
| Name | Type            | Mandatory  | Description   |
| ---- | -------------- | ---- | ----- |
| size | [Size](#size8) | No   | Size of the thumbnail.|
W
wusongqing 已提交
1490 1491 1492

**Return value**

W
wusongqing 已提交
1493 1494
| Type                           | Description                   |
| ----------------------------- | --------------------- |
W
wusongqing 已提交
1495 1496 1497 1498
| Promise&lt;image.PixelMap&gt; | Promise to return the pixel map of the thumbnail.|

**Example**

1499
```js
W
wusongqing 已提交
1500
async function example() {
1501
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1502 1503
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
W
wusongqing 已提交
1504 1505
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
G
Gloria 已提交
1506
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1507
    };
W
wusongqing 已提交
1508
    let size = { width: 720, height: 720 };
W
wusongqing 已提交
1509 1510
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1511 1512 1513 1514
    asset.getThumbnail(size).then((pixelmap) => {
        console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap));
    }).catch((error) => {
        console.error('mediaLibrary getThumbnail failed with error: ' + error);
W
wusongqing 已提交
1515
    });
G
Gloria 已提交
1516
    fetchFileResult.close();
W
wusongqing 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
}
```

### favorite<sup>8+</sup>

favorite(isFavorite: boolean, callback: AsyncCallback&lt;void&gt;): void

Favorites or unfavorites this file asset. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1532 1533 1534 1535
| Name       | Type                       | Mandatory  | Description                                |
| ---------- | ------------------------- | ---- | ---------------------------------- |
| isFavorite | boolean                   | Yes   | Whether to favorite or unfavorite the file. The value **true** means to favorite the file, and **false** means to unfavorite the file.|
| callback   | AsyncCallback&lt;void&gt; | Yes   | Void callback.                             |
W
wusongqing 已提交
1536 1537 1538

**Example**

1539
```js
W
wusongqing 已提交
1540
async function example() {
1541
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1542 1543
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1544 1545 1546
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1547 1548 1549
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1550 1551 1552 1553 1554 1555
    asset.favorite(true,(error) => {
        if (error) {
            console.error('mediaLibrary favorite failed with error: ' + error);
        } else {
            console.info('mediaLibrary favorite Successful');
        }
W
wusongqing 已提交
1556
    });
G
Gloria 已提交
1557
    fetchFileResult.close();
W
wusongqing 已提交
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
}
```

### favorite<sup>8+</sup>

favorite(isFavorite: boolean): Promise&lt;void&gt;

Favorites or unfavorites this file asset. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1573 1574 1575
| Name       | Type     | Mandatory  | Description                                |
| ---------- | ------- | ---- | ---------------------------------- |
| isFavorite | boolean | Yes   | Whether to favorite or unfavorite the file. The value **true** means to favorite the file, and **false** means to unfavorite the file.|
W
wusongqing 已提交
1576 1577 1578

**Return value**

W
wusongqing 已提交
1579 1580
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
1581 1582 1583 1584
| Promise&lt;void&gt; | Void promise.|

**Example**

1585
```js
W
wusongqing 已提交
1586
async function example() {
1587
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1588 1589
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1590 1591 1592
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1593 1594 1595
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1596 1597 1598 1599
    asset.favorite(true).then(() => {
        console.info('mediaLibrary favorite Successful');
    }).catch((error) => {
        console.error('mediaLibrary favorite failed with error: ' + error);
W
wusongqing 已提交
1600
    });
G
Gloria 已提交
1601
    fetchFileResult.close();
W
wusongqing 已提交
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
}
```

### isFavorite<sup>8+</sup>

isFavorite(callback: AsyncCallback&lt;boolean&gt;): void

Checks whether this file asset is favorited. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1617 1618 1619
| Name     | Type                          | Mandatory  | Description         |
| -------- | ---------------------------- | ---- | ----------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to return whether the file asset is favorited.|
W
wusongqing 已提交
1620 1621 1622

**Example**

1623
```js
W
wusongqing 已提交
1624
async function example() {
1625
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1626 1627
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1628 1629 1630
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1631 1632 1633
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1634 1635 1636 1637 1638
    asset.isFavorite((error, isFavorite) => {
        if (error) {
            console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
        } else {
            console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
W
wusongqing 已提交
1639 1640
        }
    });
G
Gloria 已提交
1641
    fetchFileResult.close();
W
wusongqing 已提交
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
}
```

### isFavorite<sup>8+</sup>

isFavorite():Promise&lt;boolean&gt;

Checks whether this file asset is favorited. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1657 1658
| Type                    | Description                |
| ---------------------- | ------------------ |
W
wusongqing 已提交
1659 1660 1661 1662
| Promise&lt;boolean&gt; | Promise used to return whether the file asset is favorited.|

**Example**

1663
```js
W
wusongqing 已提交
1664
async function example() {
1665
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1666 1667
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1668 1669 1670
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1671 1672 1673
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1674 1675 1676 1677
    asset.isFavorite().then((isFavorite) => {
        console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite);
    }).catch((error) => {
        console.error('mediaLibrary favoriisFavoritete failed with error: ' + error);
W
wusongqing 已提交
1678
    });
G
Gloria 已提交
1679
    fetchFileResult.close();
W
wusongqing 已提交
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696
}
```

### trash<sup>8+</sup>

trash(isTrash: boolean, callback: AsyncCallback&lt;void&gt;): void

Moves this file asset to the trash. This API uses an asynchronous callback to return the result.

Files in the trash are not actually deleted. You can set **isTrash** to **false** to restore the files from the trash.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1697 1698 1699 1700
| Name     | Type                       | Mandatory  | Description       |
| -------- | ------------------------- | ---- | --------- |
| isTrash  | boolean                   | Yes   | Whether to move the file asset to the trash. The value **true** means to move the file asset to the trash, and **false** means the opposite.|
| callback | AsyncCallback&lt;void&gt; | Yes   | Void callback.    |
W
wusongqing 已提交
1701 1702 1703

**Example**

1704
```js
W
wusongqing 已提交
1705
async function example() {
1706
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1707 1708
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1709 1710 1711
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1712 1713 1714
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1715 1716 1717 1718 1719 1720 1721 1722
    asset.trash(true, (error) => {
        if (error) {
            console.error('mediaLibrary trash failed with error: ' + error);
        } else {
            console.info('mediaLibrary trash Successful');
        }
    });
    fetchFileResult.close();
W
wusongqing 已提交
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
}
```

### trash<sup>8+</sup>

trash(isTrash: boolean): Promise&lt;void&gt;

Moves this file asset to the trash. This API uses a promise to return the result.

Files in the trash are not actually deleted. You can set **isTrash** to **false** to restore the files from the trash.

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1740 1741 1742
| Name    | Type     | Mandatory  | Description       |
| ------- | ------- | ---- | --------- |
| isTrash | boolean | Yes   | Whether to move the file asset to the trash. The value **true** means to move the file asset to the trash, and **false** means the opposite.|
W
wusongqing 已提交
1743 1744 1745

**Return value**

W
wusongqing 已提交
1746 1747
| Type                 | Description        |
| ------------------- | ---------- |
W
wusongqing 已提交
1748 1749 1750 1751
| Promise&lt;void&gt; | Void promise.|

**Example**

1752
```js
W
wusongqing 已提交
1753
async function example() {
1754
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1755 1756
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1757 1758 1759
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1760 1761 1762
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1763 1764 1765 1766
    asset.trash(true).then(() => {
        console.info('trash successfully');
    }).catch((error) => {
        console.error('trash failed with error: ' + error);
W
wusongqing 已提交
1767
    });
G
Gloria 已提交
1768
    fetchFileResult.close();
W
wusongqing 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783
}
```

### isTrash<sup>8+</sup>

isTrash(callback: AsyncCallback&lt;boolean&gt;): void

Checks whether this file asset is in the trash. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1784 1785 1786
| Name     | Type                          | Mandatory  | Description             |
| -------- | ---------------------------- | ---- | --------------- |
| callback | AsyncCallback&lt;boolean&gt; | Yes   | Callback used to return whether the file asset is in the trash. If the file asset is in the trash, **true** will be returned; otherwise, **false** will be returned.|
W
wusongqing 已提交
1787 1788 1789

**Example**

1790
```js
W
wusongqing 已提交
1791
async function example() {
1792
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1793 1794
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1795 1796 1797
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1798 1799 1800
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1801 1802 1803 1804 1805 1806
    asset.isTrash((error, isTrash) => {
        if (error) {
            console.error('Failed to get trash state failed with error: ' + error);
            return;
        }
        console.info('Get trash state successfully, isTrash result: ' + isTrash);
1807
    });
G
Gloria 已提交
1808
    fetchFileResult.close();
W
wusongqing 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
}
```

### isTrash<sup>8+</sup>

isTrash():Promise&lt;boolean&gt;

Checks whether this file asset is in the trash. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1824 1825
| Type                 | Description                  |
| ------------------- | -------------------- |
W
wusongqing 已提交
1826 1827 1828 1829
| Promise&lt;void&gt; | Promise used to return whether the file asset is in the trash. If the file asset is in the trash, **true** will be returned; otherwise, **false** will be returned.|

**Example**

1830
```js
W
wusongqing 已提交
1831
async function example() {
1832
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1833 1834
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1835 1836 1837
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1838 1839 1840
    };
    const fetchFileResult = await media.getFileAssets(getImageOp);
    const asset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
1841 1842 1843 1844
    asset.isTrash().then((isTrash) => {
        console.info('isTrash result: ' + isTrash);
    }).catch((error) => {
        console.error('isTrash failed with error: ' + error);
W
wusongqing 已提交
1845
    });
G
Gloria 已提交
1846
    fetchFileResult.close();
W
wusongqing 已提交
1847 1848 1849
}
```

W
wusongqing 已提交
1850
## FetchFileResult<sup>7+</sup>
W
wusongqing 已提交
1851 1852 1853

Implements the result set of the file retrieval operation.

W
wusongqing 已提交
1854
### getCount<sup>7+</sup>
W
wusongqing 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863

getCount(): number

Obtains the total number of files in the result set.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1864 1865
| Type    | Description      |
| ------ | -------- |
W
wusongqing 已提交
1866 1867 1868 1869
| number | Total number of files.|

**Example**

1870
```js
W
wusongqing 已提交
1871
async function example() {
1872
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1873
    let fileType = mediaLibrary.MediaType.FILE;
W
wusongqing 已提交
1874 1875 1876
    let getFileCountOneOp = {
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [fileType.toString()],
G
Gloria 已提交
1877
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1878 1879 1880
    };
    let fetchFileResult = await media.getFileAssets(getFileCountOneOp);
    const fetchCount = fetchFileResult.getCount();
G
Gloria 已提交
1881 1882
    console.info('fetchCount result: ' + fetchCount);
    fetchFileResult.close();
W
wusongqing 已提交
1883 1884 1885
}
```

W
wusongqing 已提交
1886
### isAfterLast<sup>7+</sup>
W
wusongqing 已提交
1887 1888 1889 1890 1891 1892 1893 1894 1895

isAfterLast(): boolean

Checks whether the cursor is in the last row of the result set.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1896 1897
| Type     | Description                                |
| ------- | ---------------------------------- |
W
wusongqing 已提交
1898
| boolean | Returns **true** if the cursor is in the last row of the result set; returns *false* otherwise.|
W
wusongqing 已提交
1899 1900 1901

**Example**

1902
```js
W
wusongqing 已提交
1903
async function example() {
1904
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1905 1906
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1907 1908 1909
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1910 1911 1912
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    const fetchCount = fetchFileResult.getCount();
G
Gloria 已提交
1913
    console.info('mediaLibrary fetchFileResult.getCount, count:' + fetchCount);
W
wusongqing 已提交
1914 1915
    let fileAsset = await fetchFileResult.getFirstObject();
    for (var i = 1; i < fetchCount; i++) {
G
Gloria 已提交
1916 1917 1918 1919
        fileAsset = await fetchFileResult.getNextObject();
        if(i == fetchCount - 1) {
            var result = fetchFileResult.isAfterLast();
            console.info('mediaLibrary fileAsset isAfterLast result: ' + result);
G
Gloria 已提交
1920
            fetchFileResult.close();
G
Gloria 已提交
1921
        }
W
wusongqing 已提交
1922 1923 1924 1925
    }
}
```

W
wusongqing 已提交
1926
### close<sup>7+</sup>
W
wusongqing 已提交
1927 1928 1929

close(): void

W
wusongqing 已提交
1930
Releases and invalidates this **FetchFileResult** instance. Other APIs in this instance cannot be invoked after it is released.
W
wusongqing 已提交
1931 1932 1933 1934 1935

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Example**

1936
```js
W
wusongqing 已提交
1937
async function example() {
1938
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1939 1940
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1941 1942 1943
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1944 1945 1946 1947 1948 1949
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
    fetchFileResult.close();
}
```

W
wusongqing 已提交
1950
### getFirstObject<sup>7+</sup>
W
wusongqing 已提交
1951 1952 1953 1954 1955 1956 1957 1958 1959

getFirstObject(callback: AsyncCallback&lt;FileAsset&gt;): void

Obtains the first file asset in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
1960 1961 1962
| Name  | Type                                         | Mandatory| Description                                       |
| -------- | --------------------------------------------- | ---- | ------------------------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes  | Callback used to return the first file asset.|
W
wusongqing 已提交
1963 1964 1965

**Example**

1966
```js
W
wusongqing 已提交
1967
async function example() {
1968
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
1969 1970
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
1971 1972 1973
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
1974 1975
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
1976 1977 1978 1979 1980 1981
    fetchFileResult.getFirstObject((error, fileAsset) => {
        if (error) {
            console.error('fetchFileResult getFirstObject failed with error: ' + error);
            return;
        }
        console.info('getFirstObject successfully, displayName : ' + fileAsset.displayName);
G
Gloria 已提交
1982
        fetchFileResult.close();
W
wusongqing 已提交
1983 1984 1985 1986
    })
}
```

W
wusongqing 已提交
1987
### getFirstObject<sup>7+</sup>
W
wusongqing 已提交
1988 1989 1990 1991 1992 1993 1994 1995 1996

getFirstObject(): Promise&lt;FileAsset&gt;

Obtains the first file asset in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
1997 1998 1999
| Type                                   | Description                      |
| --------------------------------------- | -------------------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the file asset.|
W
wusongqing 已提交
2000 2001 2002

**Example**

2003
```js
W
wusongqing 已提交
2004
async function example() {
2005
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2006 2007
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2008 2009 2010
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2011 2012
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2013 2014
    fetchFileResult.getFirstObject().then((fileAsset) => {
        console.info('getFirstObject successfully, displayName: ' + fileAsset.displayName);
G
Gloria 已提交
2015
        fetchFileResult.close();
G
Gloria 已提交
2016 2017
    }).catch((error) => {
        console.error('getFirstObject failed with error: ' + error);
W
wusongqing 已提交
2018 2019 2020 2021
    });
}
```

W
wusongqing 已提交
2022
### getNextObject<sup>7+</sup>
W
wusongqing 已提交
2023 2024 2025 2026

 getNextObject(callback: AsyncCallback&lt;FileAsset&gt;): void

Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result.
G
Gloria 已提交
2027 2028 2029
> **NOTE** 
> 
> Before using this API, you must use [getFirstObject](#getfirstobject7) to obtain the first file asset and then use [isAfterLast](#isafterlast7) to ensure that the cursor does not point to the last file asset in the result set.
W
wusongqing 已提交
2030 2031 2032 2033 2034

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2035 2036
| Name   | Type                                         | Mandatory| Description                                     |
| --------- | --------------------------------------------- | ---- | ----------------------------------------- |
2037
| callback| AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes  | Callback used to return the next file asset.|
W
wusongqing 已提交
2038 2039 2040

**Example**

2041
```js
W
wusongqing 已提交
2042
async function example() {
2043
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2044 2045
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2046 2047 2048
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2049 2050
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2051
    let fileAsset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
2052
    if (!fileAsset.isAfterLast) {
G
Gloria 已提交
2053 2054 2055 2056 2057 2058
        fetchFileResult.getNextObject((error, fileAsset) => {
            if (error) {
                console.error('fetchFileResult getNextObject failed with error: ' + error);
                return;
            }
            console.log('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
G
Gloria 已提交
2059
            fetchFileResult.close();
G
Gloria 已提交
2060 2061
        })
    }
W
wusongqing 已提交
2062
}
G
Gloria 已提交
2063

W
wusongqing 已提交
2064 2065
```

W
wusongqing 已提交
2066
### getNextObject<sup>7+</sup>
W
wusongqing 已提交
2067 2068 2069 2070

 getNextObject(): Promise&lt;FileAsset&gt;

Obtains the next file asset in the result set. This API uses a promise to return the result.
G
Gloria 已提交
2071 2072 2073
> **NOTE** 
> 
> Before using this API, you must use [getFirstObject](#getfirstobject7) to obtain the first file asset and then use [isAfterLast](#isafterlast7) to ensure that the cursor does not point to the last file asset in the result set.
W
wusongqing 已提交
2074 2075 2076 2077 2078

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
2079 2080 2081
| Type                                   | Description             |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.|
W
wusongqing 已提交
2082 2083 2084

**Example**

2085
```js
W
wusongqing 已提交
2086
async function example() {
2087
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2088 2089
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2090 2091 2092
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2093 2094
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2095
    let fileAsset = await fetchFileResult.getFirstObject();
G
Gloria 已提交
2096
    if (!fileAsset.isAfterLast) {
G
Gloria 已提交
2097 2098
        fetchFileResult.getNextObject().then((fileAsset) => {
            console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName);
G
Gloria 已提交
2099
            fetchFileResult.close();
G
Gloria 已提交
2100 2101 2102 2103
        }).catch((error) => {
            console.error('fetchFileResult getNextObject failed with error: ' + error);
        })
    }
W
wusongqing 已提交
2104 2105 2106
}
```

W
wusongqing 已提交
2107
### getLastObject<sup>7+</sup>
W
wusongqing 已提交
2108 2109 2110 2111 2112 2113 2114 2115 2116

getLastObject(callback: AsyncCallback&lt;FileAsset&gt;): void

Obtains the last file asset in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2117 2118 2119
| Name  | Type                                         | Mandatory| Description                       |
| -------- | --------------------------------------------- | ---- | --------------------------- |
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes  | Callback used to return the last file asset.|
W
wusongqing 已提交
2120 2121 2122

**Example**

2123
```js
W
wusongqing 已提交
2124
async function example() {
2125
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2126 2127
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2128 2129 2130
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2131 2132
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2133 2134 2135 2136 2137 2138
    fetchFileResult.getLastObject((error, fileAsset) => {
        if (error) {
            console.error('getLastObject failed with error: ' + error);
            return;
        }
        console.info('getLastObject successfully, displayName: ' + fileAsset.displayName);
G
Gloria 已提交
2139
        fetchFileResult.close();
W
wusongqing 已提交
2140 2141 2142 2143
    })
}
```

W
wusongqing 已提交
2144
### getLastObject<sup>7+</sup>
W
wusongqing 已提交
2145 2146 2147 2148 2149 2150 2151 2152 2153

getLastObject(): Promise&lt;FileAsset&gt;

Obtains the last file asset in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
2154 2155 2156
| Type                                   | Description             |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.|
W
wusongqing 已提交
2157 2158 2159

**Example**

2160
```js
W
wusongqing 已提交
2161
async function example() {
2162
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2163 2164
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2165 2166 2167
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2168 2169
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2170 2171
    fetchFileResult.getLastObject().then((fileAsset) => {
        console.info('getLastObject successfully, displayName: ' + fileAsset.displayName);
G
Gloria 已提交
2172
        fetchFileResult.close();
G
Gloria 已提交
2173 2174 2175
    }).catch((error) => {
        console.error('getLastObject failed with error: ' + error);
    });
W
wusongqing 已提交
2176 2177 2178
}
```

W
wusongqing 已提交
2179
### getPositionObject<sup>7+</sup>
W
wusongqing 已提交
2180 2181 2182 2183 2184 2185 2186 2187 2188

getPositionObject(index: number, callback: AsyncCallback&lt;FileAsset&gt;): void

Obtains a file asset with the specified index in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2189 2190
| Name      | Type                                      | Mandatory  | Description                |
| -------- | ---------------------------------------- | ---- | ------------------ |
G
Gloria 已提交
2191
| index    | number                                   | Yes   | Index of the file to obtain. The value starts from 0 and must be smaller than the **count** value of the result set.    |
W
wusongqing 已提交
2192
| callback | AsyncCallback&lt;[FileAsset](#fileasset7)&gt; | Yes   | Callback used to return the last file asset.|
W
wusongqing 已提交
2193 2194 2195

**Example**

2196
```js
W
wusongqing 已提交
2197
async function example() {
2198
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2199 2200
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2201 2202 2203
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2204 2205
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2206 2207 2208 2209 2210 2211
    fetchFileResult.getPositionObject(0, (error, fileAsset) => {
        if (error) {
            console.error('getPositionObject failed with error: ' + error);
            return;
        }
        console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
G
Gloria 已提交
2212
        fetchFileResult.close();
W
wusongqing 已提交
2213 2214 2215 2216
    })
}
```

W
wusongqing 已提交
2217
### getPositionObject<sup>7+</sup>
W
wusongqing 已提交
2218 2219 2220 2221 2222 2223 2224 2225 2226

getPositionObject(index: number): Promise&lt;FileAsset&gt;

Obtains a file asset with the specified index in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2227 2228
| Name   | Type    | Mandatory  | Description            |
| ----- | ------ | ---- | -------------- |
G
Gloria 已提交
2229
| index | number | Yes   | Index of the file to obtain. The value starts from 0 and must be smaller than the **count** value of the result set.|
W
wusongqing 已提交
2230 2231 2232

**Return value**

W
wusongqing 已提交
2233 2234 2235
| Type                                   | Description             |
| --------------------------------------- | ----------------- |
| Promise&lt;[FileAsset](#fileasset7)&gt; | Promise used to return the next file asset.|
W
wusongqing 已提交
2236 2237 2238

**Example**

2239
```js
W
wusongqing 已提交
2240
async function example() {
2241
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2242 2243
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2244 2245 2246
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2247 2248
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2249 2250
    fetchFileResult.getPositionObject(0).then((fileAsset) => {
        console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName);
G
Gloria 已提交
2251
        fetchFileResult.close();
G
Gloria 已提交
2252 2253
    }).catch((error) => {
        console.error('getPositionObject failed with error: ' + error);
2254
    });
W
wusongqing 已提交
2255 2256 2257
}
```

W
wusongqing 已提交
2258
### getAllObject<sup>7+</sup>
W
wusongqing 已提交
2259 2260 2261 2262 2263 2264 2265 2266 2267

getAllObject(callback: AsyncCallback&lt;Array&lt;FileAsset&gt;&gt;): void

Obtains all the file assets in the result set. This API uses an asynchronous callback to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2268 2269
| Name      | Type                                      | Mandatory  | Description                  |
| -------- | ---------------------------------------- | ---- | -------------------- |
G
Gloria 已提交
2270
| callback | AsyncCallback&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | Yes   | Callback used to return the file assets.|
W
wusongqing 已提交
2271 2272 2273

**Example**

2274
```js
W
wusongqing 已提交
2275
async function example() {
2276
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2277 2278
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2279 2280 2281
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2282 2283
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2284 2285 2286
    fetchFileResult.getAllObject((error, fileAssetList) => {
        if (error) {
           console.error('getAllObject failed with error: ' + error);
W
wusongqing 已提交
2287
           return;
2288 2289
        }
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
G
Gloria 已提交
2290
            console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
G
Gloria 已提交
2291 2292
        }
        fetchFileResult.close();
W
wusongqing 已提交
2293 2294 2295 2296
    })
}
```

W
wusongqing 已提交
2297
### getAllObject<sup>7+</sup>
W
wusongqing 已提交
2298 2299 2300 2301 2302 2303 2304 2305 2306

getAllObject(): Promise&lt;Array&lt;FileAsset&gt;&gt;

Obtains all the file assets in the result set. This API uses a promise to return the result.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
2307 2308
| Type                                    | Description                 |
| ---------------------------------------- | --------------------- |
G
Gloria 已提交
2309
| Promise&lt;Array&lt;[FileAsset](#fileasset7)&gt;&gt; | Promise used to return the file assets.|
W
wusongqing 已提交
2310 2311 2312

**Example**

2313
```js
W
wusongqing 已提交
2314
async function example() {
2315
    let fileKeyObj = mediaLibrary.FileKey;
W
wusongqing 已提交
2316 2317
    let imageType = mediaLibrary.MediaType.IMAGE;
    let getImageOp = {
G
Gloria 已提交
2318 2319 2320
        selections: fileKeyObj.MEDIA_TYPE + '= ?',
        selectionArgs: [imageType.toString()],
        order: fileKeyObj.DATE_ADDED + ' DESC',
W
wusongqing 已提交
2321 2322
    };
    let fetchFileResult = await media.getFileAssets(getImageOp);
G
Gloria 已提交
2323 2324 2325 2326
    fetchFileResult.getAllObject().then((fileAssetList) => {
        for (let i = 0; i < fetchFileResult.getCount(); i++) {
            console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName);
        } 
G
Gloria 已提交
2327
        fetchFileResult.close();
G
Gloria 已提交
2328 2329 2330
    }).catch((error) => {
        console.error('getAllObject failed with error: ' + error);
    });
W
wusongqing 已提交
2331 2332 2333
}
```

W
wusongqing 已提交
2334
## Album<sup>7+</sup>
W
wusongqing 已提交
2335 2336 2337

Provides APIs to implement a physical album.

W
wusongqing 已提交
2338
### Attributes
W
wusongqing 已提交
2339 2340 2341

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

W
wusongqing 已提交
2342 2343
| Name          | Type   | Readable  | Writable  | Description     |
| ------------ | ------ | ---- | ---- | ------- |
W
wusongqing 已提交
2344 2345 2346
| albumId | number | Yes   | No   | Album ID.   |
| albumName | string | Yes   | Yes   | Album name.   |
| albumUri<sup>8+</sup> | string | Yes   | No   | Album URI.  |
W
wusongqing 已提交
2347
| dateModified | number | Yes   | No   | Date when the album was modified.   |
W
wusongqing 已提交
2348 2349 2350
| count<sup>8+</sup> | number | Yes   | No   | Number of files in the album.|
| relativePath<sup>8+</sup> | string | Yes   | No   | Relative path of the album.   |
| coverUri<sup>8+</sup> | string | Yes   | No   | URI of the cover file of the album.|
W
wusongqing 已提交
2351 2352 2353 2354 2355

### commitModify<sup>8+</sup>

commitModify(callback: AsyncCallback&lt;void&gt;): void

W
wusongqing 已提交
2356
Commits the modification in the album attributes to the database. This API uses an asynchronous callback to return the result.
W
wusongqing 已提交
2357 2358 2359 2360 2361 2362 2363

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2364 2365 2366
| Name  | Type                     | Mandatory| Description      |
| -------- | ------------------------- | ---- | ---------- |
| callback | AsyncCallback&lt;void&gt; | Yes  | Void callback.|
W
wusongqing 已提交
2367 2368 2369

**Example**

2370
```js
W
wusongqing 已提交
2371 2372 2373 2374 2375 2376 2377 2378
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
G
Gloria 已提交
2379 2380 2381 2382 2383 2384
    album.commitModify((error) => {
        if (error) {
            console.error('commitModify failed with error: ' + error);
            return;
        }
        console.info('commitModify successful.');
W
wusongqing 已提交
2385 2386 2387 2388 2389 2390 2391 2392
    })
}
```

### commitModify<sup>8+</sup>

commitModify(): Promise&lt;void&gt;

W
wusongqing 已提交
2393
Commits the modification in the album attributes to the database. This API uses a promise to return the result.
W
wusongqing 已提交
2394 2395 2396 2397 2398 2399 2400

**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Return value**

W
wusongqing 已提交
2401 2402
| Type                 | Description          |
| ------------------- | ------------ |
W
wusongqing 已提交
2403 2404 2405 2406
| Promise&lt;void&gt; | Void promise.|

**Example**

2407
```js
W
wusongqing 已提交
2408 2409 2410 2411 2412 2413 2414 2415
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
    album.albumName = 'hello';
G
Gloria 已提交
2416 2417 2418 2419
    album.commitModify().then(() => {
        console.info('commitModify successfully');
    }).catch((error) => {
        console.error('commitModify failed with error: ' + error);
W
wusongqing 已提交
2420 2421 2422 2423
    });
}
```

W
wusongqing 已提交
2424
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435

getFileAssets(options: MediaFetchOptions, callback: AsyncCallback&lt;FetchFileResult&gt;): void

Obtains the file assets in this album. This API uses an asynchronous callback to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2436 2437 2438 2439
| Name  | Type                                               | Mandatory| Description                               |
| -------- | --------------------------------------------------- | ---- | ----------------------------------- |
| options  | [MediaFetchOptions](#mediafetchoptions7)            | Yes  | Options for fetching the files.                     |
| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes  | Callback used to return the file assets.|
W
wusongqing 已提交
2440 2441 2442

**Example**

2443
```js
W
wusongqing 已提交
2444 2445 2446 2447 2448
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
W
wusongqing 已提交
2449
    let fileNoArgsfetchOp = {
G
Gloria 已提交
2450 2451
        selections: '',
        selectionArgs: [],
W
wusongqing 已提交
2452
    }
G
Gloria 已提交
2453
    // Obtain the albums that meet the retrieval options and return the album list.
W
wusongqing 已提交
2454 2455
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
G
Gloria 已提交
2456 2457 2458 2459 2460 2461
    // Obtain an album from the album list and obtain all media assets that meet the retrieval options in the album.
    album.getFileAssets(fileNoArgsfetchOp, (error, fetchFileResult) => {
        if (error) {
            console.error('album getFileAssets failed with error: ' + error);
            return;
        }
G
Gloria 已提交
2462
        let count = fetchFileResult.getCount();
G
Gloria 已提交
2463
        console.info('album getFileAssets successfully, count: ' + count);
G
Gloria 已提交
2464
        fetchFileResult.close();
G
Gloria 已提交
2465
    });
W
wusongqing 已提交
2466 2467 2468
}
```

W
wusongqing 已提交
2469
### getFileAssets<sup>7+</sup>
W
wusongqing 已提交
2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480

 getFileAssets(options?: MediaFetchOptions): Promise&lt;FetchFileResult&gt;

Obtains the file assets in this album. This API uses a promise to return the result.

**Required permissions**: ohos.permission.READ_MEDIA

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

**Parameters**

W
wusongqing 已提交
2481 2482 2483
| Name | Type                                    | Mandatory| Description          |
| ------- | ---------------------------------------- | ---- | -------------- |
| options | [MediaFetchOptions](#mediafetchoptions7) | No  | Options for fetching the files.|
W
wusongqing 已提交
2484 2485 2486

**Return value**

W
wusongqing 已提交
2487 2488 2489
| Type                                         | Description                     |
| --------------------------------------------- | ------------------------- |
| Promise<[FetchFileResult](#fetchfileresult7)> | Promise used to return the file assets.|
W
wusongqing 已提交
2490 2491 2492

**Example**

2493
```js
W
wusongqing 已提交
2494 2495 2496 2497 2498
async function example() {
    let AlbumNoArgsfetchOp = {
        selections: '',
        selectionArgs: [],
    };
G
Gloria 已提交
2499
    let fileNoArgsfetchOp = {
G
Gloria 已提交
2500 2501
        selections: '',
        selectionArgs: [],
2502
    };
G
Gloria 已提交
2503
    // Obtain the albums that meet the retrieval options and return the album list.
W
wusongqing 已提交
2504 2505
    const albumList = await media.getAlbums(AlbumNoArgsfetchOp);
    const album = albumList[0];
G
Gloria 已提交
2506
    // Obtain an album from the album list and obtain all media assets that meet the retrieval options in the album.
G
Gloria 已提交
2507 2508
    album.getFileAssets(fileNoArgsfetchOp).then((fetchFileResult) => {
        let count = fetchFileResult.getCount();
G
Gloria 已提交
2509
        console.info('album getFileAssets successfully, count: ' + count);
G
Gloria 已提交
2510
        fetchFileResult.close();
G
Gloria 已提交
2511 2512
    }).catch((error) => {
        console.error('album getFileAssets failed with error: ' + error);
W
wusongqing 已提交
2513 2514 2515 2516 2517 2518 2519
    });
}
```

## PeerInfo<sup>8+</sup>

Describes information about a registered device.
2520 2521

**System API**: This is a system API.
W
wusongqing 已提交
2522

W
wusongqing 已提交
2523
**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore
W
wusongqing 已提交
2524

W
wusongqing 已提交
2525 2526 2527 2528 2529 2530
| Name      | Type                      | Readable| Writable| Description            |
| ---------- | -------------------------- | ---- | ---- | ---------------- |
| deviceName | string                     | Yes  | No  | Name of the registered device.  |
| networkId  | string                     | Yes  | No  | Network ID of the registered device.|
| deviceType | [DeviceType](#devicetype8) | Yes  | No  | Type of the registered device.        |
| isOnline   | boolean                    | Yes  | No  | Whether the registered device is online.        |
W
wusongqing 已提交
2531 2532 2533



W
wusongqing 已提交
2534
## MediaType<sup>8+</sup>
W
wusongqing 已提交
2535 2536 2537 2538 2539

Enumerates media types.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

2540 2541 2542 2543 2544 2545
| Name |  Value|  Description|
| ----- |  ---- | ---- |
| FILE  |  0 | File.|
| IMAGE |  1 | Image.|
| VIDEO |  2 | Video.|
| AUDIO |  3 | Audio.|
W
wusongqing 已提交
2546

W
wusongqing 已提交
2547
## FileKey<sup>8+</sup>
W
wusongqing 已提交
2548 2549 2550

Enumerates key file information.

G
gloria 已提交
2551 2552 2553
> **NOTE**
> The **bucket_id** field may change after file rename or movement. Therefore, you must obtain the field again before using it.

W
wusongqing 已提交
2554 2555
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

2556
| Name         | Value             | Description                                                      |
W
wusongqing 已提交
2557
| ------------- | ------------------- | ---------------------------------------------------------- |
G
Gloria 已提交
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576
| ID            | 'file_id'             | File ID.                                                  |
| RELATIVE_PATH | 'relative_path'       | Relative public directory of the file.                                          |
| DISPLAY_NAME  | 'display_name'        | Display file name.                                                  |
| PARENT        | 'parent'              | Parent directory ID.                                                  |
| MIME_TYPE     | 'mime_type'           | Extended file attributes, such as image/, video/, and file/*.                                            |
| MEDIA_TYPE    | 'media_type'          | Media type.                                                  |
| SIZE          | 'size'                | File size, in bytes.                                    |
| DATE_ADDED    | 'date_added'          | Date when the file was added. The value is the number of seconds elapsed since the Epoch time.            |
| DATE_MODIFIED | 'date_modified'       | Date when the file content (not the file name) was last modified. The value is the number of seconds elapsed since the Epoch time.|
| DATE_TAKEN    | 'date_taken'          | Date when the file (photo) was taken. The value is the number of seconds elapsed since the Epoch time.            |
| TITLE         | 'title'               | Title in the file.                                                  |
| ARTIST        | 'artist'              | Artist of the file.                                                      |
| AUDIOALBUM    | 'audio_album'         | Audio album.                                                      |
| DURATION      | 'duration'            | Duration, in ms.                                      |
| WIDTH         | 'width'               | Image width, in pixels.                                    |
| HEIGHT        | 'height'              | Image height, in pixels.                                    |
| ORIENTATION   | 'orientation'         | Image display direction (clockwise rotation angle, for example, 0, 90, and 180, in degrees).|
| ALBUM_ID      | 'bucket_id'           | ID of the album to which the file belongs.                                      |
| ALBUM_NAME    | 'bucket_display_name' | Name of the album to which the file belongs.                                        |
W
wusongqing 已提交
2577

W
wusongqing 已提交
2578
## DirectoryType<sup>8+</sup>
W
wusongqing 已提交
2579 2580 2581 2582 2583

Enumerates directory types.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

2584 2585 2586 2587 2588 2589 2590 2591
| Name         | Value|  Description              |
| ------------- | --- | ------------------ |
| DIR_CAMERA    |  0 | Directory of camera files.|
| DIR_VIDEO     |  1 |  Directory of video files.      |
| DIR_IMAGE     |  2 | Directory of image files.      |
| DIR_AUDIO     |  3 | Directory of audio files.      |
| DIR_DOCUMENTS |  4 | Directory of documents.      |
| DIR_DOWNLOAD  |  5 |  Download directory.      |
W
wusongqing 已提交
2592

W
wusongqing 已提交
2593
## DeviceType<sup>8+</sup>
W
wusongqing 已提交
2594 2595

Enumerates device types.
2596 2597

**System API**: This is a system API.
W
wusongqing 已提交
2598

W
wusongqing 已提交
2599
**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore
W
wusongqing 已提交
2600

2601 2602 2603 2604 2605 2606 2607 2608 2609
| Name        |  Value| Description      |
| ------------ | --- | ---------- |
| TYPE_UNKNOWN |  0 | Unknown.|
| TYPE_LAPTOP  |  1 | Laptop.|
| TYPE_PHONE   |  2 | Phone.      |
| TYPE_TABLET  |  3 | Tablet.  |
| TYPE_WATCH   |  4 | Smart watch.  |
| TYPE_CAR     |  5 | Vehicle-mounted device.  |
| TYPE_TV      |  6 | TV.  |
W
wusongqing 已提交
2610

W
wusongqing 已提交
2611
## MediaFetchOptions<sup>7+</sup>
W
wusongqing 已提交
2612 2613 2614 2615 2616

Describes options for fetching media files.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

2617 2618
| Name                   | Type               | Readable| Writable| Description                                                        |
| ----------------------- | ------------------- | ---- | ---- | ------------------------------------------------------------ |
G
Gloria 已提交
2619
| selections              | string              | Yes  | Yes  | Conditions for fetching files. The enumerated values in [FileKey](#filekey8) are used as the column names of the conditions. Example:<br>selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ? OR ' + mediaLibrary.FileKey.MEDIA_TYPE + '= ?', |
2620
| selectionArgs           | Array&lt;string&gt; | Yes  | Yes  | Value of the condition, which corresponds to the value of the condition column in **selections**.<br>Example:<br>selectionArgs: [mediaLibrary.MediaType.IMAGE.toString(), mediaLibrary.MediaType.VIDEO.toString()], |
G
Gloria 已提交
2621
| order                   | string              | Yes  | Yes  | Sorting mode of the search results, which can be ascending or descending. The enumerated values in [FileKey](#filekey8) are used as the columns for sorting the search results. Example:<br>Ascending: order: mediaLibrary.FileKey.DATE_ADDED + ' ASC'<br>Descending: order: mediaLibrary.FileKey.DATE_ADDED + ' DESC'|
2622 2623 2624
| uri<sup>8+</sup>        | string              | Yes  | Yes  | File URI.                                                     |
| networkId<sup>8+</sup>  | string              | Yes  | Yes  | Network ID of the registered device.                                              |
| extendArgs<sup>8+</sup> | string              | Yes  | Yes  | Extended parameters for fetching the files. Currently, no extended parameters are available.                        |
W
wusongqing 已提交
2625 2626 2627 2628

## Size<sup>8+</sup>

Describes the image size.
2629

W
wusongqing 已提交
2630
**System capability**: SystemCapability.Multimedia.MediaLibrary.Core
W
wusongqing 已提交
2631

W
wusongqing 已提交
2632 2633 2634 2635
| Name    | Type    | Readable  | Writable  | Description      |
| ------ | ------ | ---- | ---- | -------- |
| width  | number | Yes   | Yes   | Image width, in pixels.|
| height | number | Yes   | Yes   | Image height, in pixels.|
W
wusongqing 已提交
2636

G
Gloria 已提交
2637
## MediaAssetOption
W
wusongqing 已提交
2638 2639 2640 2641 2642 2643

Implements the media asset option.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core


2644 2645 2646 2647 2648
| Name        | Type  | Readable| Writable| Description                                                        |
| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ |
| src          | string | Yes  | Yes  | Application sandbox oath of the local file.                                      |
| mimeType     | string | Yes  | Yes  | Multipurpose Internet Mail Extensions (MIME) type of the media.<br>The value can be 'image/\*', 'video/\*', 'audio/\*' or 'file\*'.|
| relativePath | string | Yes  | Yes  | Custom path for storing media assets, for example, 'Pictures/'. If this parameter is unspecified, media assets are stored in the default path.<br> Default path of images: 'Pictures/'<br> Default path of videos: 'Videos/'<br> Default path of audios: 'Audios/'<br> Default path of files: 'Documents/'|
W
wusongqing 已提交
2649

G
Gloria 已提交
2650
## MediaSelectOption
W
wusongqing 已提交
2651 2652 2653 2654 2655

Describes media selection option.

**System capability**: SystemCapability.Multimedia.MediaLibrary.Core

2656 2657
| Name   | Type    | Readable| Writable| Description                  |
| ----- | ------ | ---- | ---- | -------------------- |
G
gloria 已提交
2658
| type  | 'image' &#124; 'video' &#124; 'media' | Yes   | Yes | Media type, which can be **image**, **media**, or **video**. Currently, only **media** is supported.|
G
Gloria 已提交
2659
| count | number | Yes   | Yes | Maximum number of media assets that can be selected. The value starts from 1, which indicates that one media asset can be selected.           |