1# @ohos.multimedia.medialibrary (Media Library Management) 2 3> **NOTE** 4> 5> - The APIs of this module are supported since API version 6. Updates will be marked with a superscript to indicate their earliest API version. 6> - This API is deprecated since API version 9 and will be retained until API version 13. 7> - 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). 8> - 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). 9 10## Modules to Import 11```js 12import mediaLibrary from '@ohos.multimedia.mediaLibrary'; 13``` 14 15## mediaLibrary.getMediaLibrary<sup>8+</sup> 16 17getMediaLibrary(context: Context): MediaLibrary 18 19Obtains a **MediaLibrary** instance, which is used to access and modify personal media data such as audios, videos, images, and documents. 20 21This API can be used only in the stage model. 22 23**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 24 25**Parameters** 26 27| Name | Type | Mandatory| Description | 28| ------- | ------- | ---- | -------------------------- | 29| context | Context | Yes | Context of the ability.| 30 31**Return value** 32 33| Type | Description | 34| ----------------------------- | :---- | 35| [MediaLibrary](#medialibrary) | **MediaLibrary** instance.| 36 37**Example (from API version 9)** 38 39```ts 40// Obtain a MediaLibrary instance. The instance obtained here is used in later. 41const context = getContext(this); 42let media = mediaLibrary.getMediaLibrary(context); 43``` 44 45**Example (API version 8)** 46 47```js 48import featureAbility from '@ohos.ability.featureAbility'; 49 50let context = featureAbility.getContext(); 51let media = mediaLibrary.getMediaLibrary(context); 52``` 53 54## mediaLibrary.getMediaLibrary 55 56getMediaLibrary(): MediaLibrary 57 58Obtains a **MediaLibrary** instance, which is used to access and modify personal media data such as audios, videos, images, and documents. 59 60This API can be used only in the FA model. 61 62**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 63 64**Return value** 65 66| Type | Description | 67| ----------------------------- | :--------- | 68| [MediaLibrary](#medialibrary) | **MediaLibrary** instance.| 69 70**Example** 71 72```js 73let media = mediaLibrary.getMediaLibrary(); 74``` 75 76## MediaLibrary 77 78### getFileAssets<sup>7+</sup> 79 80 81getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): void 82 83Obtains file assets (also called files). This API uses an asynchronous callback to return the result. 84 85**Required permissions**: ohos.permission.READ_MEDIA 86 87**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 88 89**Parameters** 90 91| Name | Type | Mandatory| Description | 92| -------- | --------------------------------------------------- | ---- | --------------------------------- | 93| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the files. | 94| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Asynchronous callback of **FetchFileResult**.| 95 96**Example** 97 98```js 99async function example() { 100 let fileKeyObj = mediaLibrary.FileKey; 101 let imageType = mediaLibrary.MediaType.IMAGE; 102 // Create options for fetching the files. The options are used to obtain files of the image type. 103 let imagesFetchOp = { 104 selections: fileKeyObj.MEDIA_TYPE + '= ?', 105 selectionArgs: [imageType.toString()], 106 }; 107 // Obtain the files in asynchronous callback mode. 108 media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => { 109 // Check whether the result set of the obtained files is undefined. If yes, the API call fails. 110 if (fetchFileResult == undefined) { 111 console.error('get fetchFileResult failed with error: ' + error); 112 return; 113 } 114 // Obtain the total number of files in the result set. 115 const count = fetchFileResult.getCount(); 116 // Check whether the number is less than 0. If yes, the API call fails. 117 if (count < 0) { 118 console.error('get count from fetchFileResult failed, count: ' + count); 119 return; 120 } 121 // 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. 122 if (count == 0) { 123 console.info('The count of fetchFileResult is zero'); 124 return; 125 } 126 console.info('Get fetchFileResult successfully, count: ' + count); 127 // Obtain the first file in the result set in asynchronous callback mode. 128 fetchFileResult.getFirstObject((error, fileAsset) => { 129 // Check whether the first file is undefined. If yes, the API call fails. 130 if (fileAsset == undefined) { 131 console.error('get first object failed with error: ' + error); 132 return; 133 } 134 console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName); 135 // Call getNextObject to obtain the next file until the last one. 136 for (let i = 1; i < count; i++) { 137 let fileAsset = await fetchFileResult.getNextObject(); 138 console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); 139 } 140 // Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called. 141 fetchFileResult.close(); 142 }); 143 }); 144} 145``` 146 147### getFileAssets<sup>7+</sup> 148 149getFileAssets(options: MediaFetchOptions): Promise<FetchFileResult> 150 151Obtains file assets. This API uses a promise to return the result. 152 153**Required permissions**: ohos.permission.READ_MEDIA 154 155**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 156 157**Parameters** 158 159| Name | Type | Mandatory| Description | 160| ------- | ---------------------------------------- | ---- | ------------ | 161| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the files.| 162 163**Return value** 164 165| Type | Description | 166| ------------------------------------ | -------------- | 167| [FetchFileResult](#fetchfileresult7) | Result set of the file retrieval operation.| 168 169**Example** 170 171```js 172async function example() { 173 let fileKeyObj = mediaLibrary.FileKey; 174 let imageType = mediaLibrary.MediaType.IMAGE; 175 // Create options for fetching the files. The options are used to obtain files of the image type. 176 let imagesFetchOp = { 177 selections: fileKeyObj.MEDIA_TYPE + '= ?', 178 selectionArgs: [imageType.toString()], 179 }; 180 // Obtain the files in promise mode. 181 media.getFileAssets(imagesFetchOp).then((fetchFileResult) => { 182 // Obtain the total number of files in the result set. 183 const count = fetchFileResult.getCount(); 184 // Check whether the number is less than 0. If yes, the API call fails. 185 if (count < 0) { 186 console.error('get count from fetchFileResult failed, count: ' + count); 187 return; 188 } 189 // 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. 190 if (count == 0) { 191 console.info('The count of fetchFileResult is zero'); 192 return; 193 } 194 console.info('Get fetchFileResult successfully, count: ' + count); 195 // Obtain the first file in the result set in promise mode. 196 fetchFileResult.getFirstObject().then((fileAsset) => { 197 console.info('fileAsset.displayName ' + '0 : ' + fileAsset.displayName); 198 // Call getNextObject to obtain the next file until the last one. 199 for (let i = 1; i < count; i++) { 200 let fileAsset = await fetchFileResult.getNextObject(); 201 console.info('fileAsset.displayName ' + i + ': ' + fileAsset.displayName); 202 } 203 // Release the FetchFileResult instance and invalidate it. Other APIs can no longer be called. 204 fetchFileResult.close(); 205 }).catch((error) => { 206 // Calling getFirstObject fails. 207 console.error('get first object failed with error: ' + error); 208 }); 209 }).catch((error) => { 210 // Calling getFileAssets fails. 211 console.error('get file assets failed with error: ' + error); 212 }); 213} 214``` 215 216### on<sup>8+</sup> 217 218on(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback: Callback<void>): void 219 220Subscribes to the media library changes. This API uses an asynchronous callback to return the result. 221 222**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 223 224**Parameters** 225 226| Name | Type | Mandatory | Description | 227| -------- | -------------------- | ---- | ---------------------------------------- | 228| type | 'deviceChange'|<br>'albumChange'|<br>'imageChange'|<br>'audioChange'|<br>'videoChange'|<br>'fileChange'|<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<void> | Yes | Void callback. | 230 231**Example** 232 233```js 234media.on('imageChange', () => { 235 // image file had changed, do something 236}) 237``` 238### off<sup>8+</sup> 239 240off(type: 'deviceChange'|'albumChange'|'imageChange'|'audioChange'|'videoChange'|'fileChange'|'remoteFileChange', callback?: Callback<void>): void 241 242Unsubscribes from the media library changes. This API uses an asynchronous callback to return the result. 243 244**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 245 246**Parameters** 247 248| Name | Type | Mandatory | Description | 249| -------- | -------------------- | ---- | ---------------------------------------- | 250| type | 'deviceChange'|<br>'albumChange'|<br>'imageChange'|<br>'audioChange'|<br>'videoChange'|<br>'fileChange'|<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<void> | No | Void callback. | 252 253**Example** 254 255```js 256media.off('imageChange', () => { 257 // Stop listening successfully. 258}) 259``` 260 261### createAsset<sup>8+</sup> 262 263createAsset(mediaType: MediaType, displayName: string, relativePath: string, callback: AsyncCallback<FileAsset>): void 264 265Creates a media asset. This API uses an asynchronous callback to return the result. 266 267**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 268 269**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 270 271**Parameters** 272 273| Name | Type | Mandatory| Description | 274| ------------ | --------------------------------------- | ---- | ------------------------------------------------------------ | 275| mediaType | [MediaType](#mediatype8) | Yes | Media type. | 276| displayName | string | Yes | Display file name. | 277| relativePath | string | Yes | Path for storing the file. You can use [getPublicDirectory](#getpublicdirectory8) to obtain the paths for storing different types of files.| 278| callback | AsyncCallback<[FileAsset](#fileasset7)> | Yes | Asynchronous callback for **FileAsset**. | 279 280**Example** 281 282```js 283async function example() { 284 // Create an image file in callback mode. 285 let mediaType = mediaLibrary.MediaType.IMAGE; 286 let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; 287 const path = await media.getPublicDirectory(DIR_IMAGE); 288 media.createAsset(mediaType, 'imageCallBack.jpg', path + 'myPicture/', (error, fileAsset) => { 289 if (fileAsset != undefined) { 290 console.info('createAsset successfully, message'); 291 } else { 292 console.error('createAsset failed with error: ' + error); 293 } 294 }); 295} 296``` 297 298### createAsset<sup>8+</sup> 299 300createAsset(mediaType: MediaType, displayName: string, relativePath: string): Promise<FileAsset> 301 302Creates a media asset. This API uses a promise to return the result. 303 304**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 305 306**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 307 308**Parameters** 309 310| Name | Type | Mandatory| Description | 311| ------------ | ------------------------ | ---- | ------------------------------------------------------------ | 312| mediaType | [MediaType](#mediatype8) | Yes | Media type. | 313| displayName | string | Yes | Display file name. | 314| 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.| 315 316**Return value** 317 318| Type | Description | 319| ------------------------ | ----------------- | 320| [FileAsset](#fileasset7) | Media data (FileAsset).| 321 322**Example** 323 324```js 325async function example() { 326 // Create an image file in promise mode. 327 let mediaType = mediaLibrary.MediaType.IMAGE; 328 let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; 329 const path = await media.getPublicDirectory(DIR_IMAGE); 330 media.createAsset(mediaType, 'imagePromise.jpg', path + 'myPicture/').then((fileAsset) => { 331 console.info('createAsset successfully, message = ' + JSON.stringify(fileAsset)); 332 }).catch((error) => { 333 console.error('createAsset failed with error: ' + error); 334 }); 335} 336``` 337 338### deleteAsset<sup>8+</sup> 339 340deleteAsset(uri: string): Promise\<void> 341 342Deletes a file asset. This API uses a promise to return the result. 343 344**System API**: This is a system API. 345 346**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 347 348**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 349 350**Parameters** 351 352| Name | Type | Mandatory | Description | 353| -------- | ---------------------------- | ---- | --------------- | 354| uri | string | Yes | URI of the file asset to delete.| 355 356**Return value** 357| Type | Description | 358| ------------------- | -------------------- | 359| Promise<void> | Promise used to return the result.| 360 361**Example** 362 363```js 364async function example() { 365 let fileKeyObj = mediaLibrary.FileKey; 366 let fileType = mediaLibrary.MediaType.FILE; 367 let option = { 368 selections: fileKeyObj.MEDIA_TYPE + '= ?', 369 selectionArgs: [fileType.toString()], 370 }; 371 const fetchFileResult = await media.getFileAssets(option); 372 let asset = await fetchFileResult.getFirstObject(); 373 if (asset == undefined) { 374 console.error('asset not exist'); 375 return; 376 } 377 media.deleteAsset(asset.uri).then(() => { 378 console.info('deleteAsset successfully'); 379 }).catch((error) => { 380 console.error('deleteAsset failed with error: ' + error); 381 }); 382 fetchFileResult.close(); 383} 384``` 385 386### deleteAsset<sup>8+</sup> 387deleteAsset(uri: string, callback: AsyncCallback\<void>): void 388 389Deletes a file asset. This API uses an asynchronous callback to return the result. 390 391**System API**: This is a system API. 392 393**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 394 395**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 396 397**Parameters** 398 399| Name | Type | Mandatory | Description | 400| -------- | ---------------------------- | ---- | --------------- | 401| uri | string | Yes | URI of the file asset to delete.| 402|callback |AsyncCallback\<void>| Yes |Callback used to return the result.| 403 404**Example** 405 406```js 407async function example() { 408 let fileKeyObj = mediaLibrary.FileKey; 409 let fileType = mediaLibrary.MediaType.FILE; 410 let option = { 411 selections: fileKeyObj.MEDIA_TYPE + '= ?', 412 selectionArgs: [fileType.toString()], 413 }; 414 const fetchFileResult = await media.getFileAssets(option); 415 let asset = await fetchFileResult.getFirstObject(); 416 if (asset == undefined) { 417 console.error('asset not exist'); 418 return; 419 } 420 media.deleteAsset(asset.uri, (error) => { 421 if (error != undefined) { 422 console.error('deleteAsset failed with error: ' + error); 423 } else { 424 console.info('deleteAsset successfully'); 425 } 426 }); 427 fetchFileResult.close(); 428} 429``` 430 431### getPublicDirectory<sup>8+</sup> 432 433getPublicDirectory(type: DirectoryType, callback: AsyncCallback<string>): void 434 435Obtains a public directory. This API uses an asynchronous callback to return the result. 436 437**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 438 439**Parameters** 440 441| Name | Type | Mandatory| Description | 442| -------- | -------------------------------- | ---- | ------------------------- | 443| type | [DirectoryType](#directorytype8) | Yes | Type of the public directory. | 444| callback | AsyncCallback<string> | Yes | Callback used to return the public directory.| 445 446**Example** 447 448```js 449let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; 450media.getPublicDirectory(DIR_CAMERA, (error, dicResult) => { 451 if (dicResult == 'Camera/') { 452 console.info('getPublicDirectory DIR_CAMERA successfully'); 453 } else { 454 console.error('getPublicDirectory DIR_CAMERA failed with error: ' + error); 455 } 456}); 457``` 458 459### getPublicDirectory<sup>8+</sup> 460 461getPublicDirectory(type: DirectoryType): Promise<string> 462 463Obtains a public directory. This API uses a promise to return the result. 464 465**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 466 467**Parameters** 468 469| Name| Type | Mandatory| Description | 470| ------ | -------------------------------- | ---- | ------------ | 471| type | [DirectoryType](#directorytype8) | Yes | Type of the public directory.| 472 473**Return value** 474 475| Type | Description | 476| ---------------- | ---------------- | 477| Promise\<string> | Promise used to return the public directory.| 478 479**Example** 480 481```js 482async function example() { 483 let DIR_CAMERA = mediaLibrary.DirectoryType.DIR_CAMERA; 484 media.getPublicDirectory(DIR_CAMERA).then((dicResult) => { 485 if (dicResult == 'Camera/') { 486 console.info('getPublicDirectory DIR_CAMERA successfully'); 487 } else { 488 console.error('getPublicDirectory DIR_CAMERA failed'); 489 } 490 }).catch((error) => { 491 console.error('getPublicDirectory failed with error: ' + error); 492 }); 493} 494``` 495 496### getAlbums<sup>7+</sup> 497 498getAlbums(options: MediaFetchOptions, callback: AsyncCallback<Array<Album>>): void 499 500Obtains the albums. This API uses an asynchronous callback to return the result. 501 502**Required permissions**: ohos.permission.READ_MEDIA 503 504**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 505 506**Parameters** 507 508| Name | Type | Mandatory| Description | 509| -------- | -------------------------------------------- | ---- | --------------------------- | 510| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the albums. | 511| callback | AsyncCallback<Array<[Album](#album7)>> | Yes | Callback used to return the albums.| 512 513**Example** 514 515```js 516async function example() { 517 let AlbumNoArgsfetchOp = { 518 selections: '', 519 selectionArgs: [], 520 }; 521 media.getAlbums(AlbumNoArgsfetchOp, (error, albumList) => { 522 if (albumList != undefined) { 523 console.info('getAlbums successfully: ' + JSON.stringify(albumList)); 524 } else { 525 console.error('getAlbums failed with error: ' + error); 526 } 527 }) 528} 529``` 530 531### getAlbums<sup>7+</sup> 532 533getAlbums(options: MediaFetchOptions): Promise<Array<Album>> 534 535Obtains the albums. This API uses a promise to return the result. 536 537**Required permissions**: ohos.permission.READ_MEDIA 538 539**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 540 541**Parameters** 542 543| Name | Type | Mandatory| Description | 544| ------- | ---------------------------------------- | ---- | ------------ | 545| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the albums.| 546 547**Return value** 548 549| Type | Description | 550| -------------------------------- | ------------- | 551| Promise<Array<[Album](#album7)>> | Promise used to return the albums.| 552 553**Example** 554 555```js 556async function example() { 557 let AlbumNoArgsfetchOp = { 558 selections: '', 559 selectionArgs: [], 560 }; 561 media.getAlbums(AlbumNoArgsfetchOp).then((albumList) => { 562 console.info('getAlbums successfully: ' + JSON.stringify(albumList)); 563 }).catch((error) => { 564 console.error('getAlbums failed with error: ' + error); 565 }); 566} 567``` 568 569### release<sup>8+</sup> 570 571release(callback: AsyncCallback<void>): void 572 573Releases this **MediaLibrary** instance. 574Call this API when you no longer need to use the APIs in the **MediaLibrary** instance. 575 576**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 577 578**Parameters** 579 580| Name | Type | Mandatory | Description | 581| -------- | ------------------------- | ---- | ---------- | 582| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 583 584**Example** 585 586```js 587media.release(() => { 588 // do something 589}); 590``` 591 592### release<sup>8+</sup> 593 594release(): Promise<void> 595 596Releases this **MediaLibrary** instance. 597Call this API when you no longer need to use the APIs in the **MediaLibrary** instance. 598 599**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 600 601**Return value** 602 603| Type | Description | 604| ------------------- | -------------------- | 605| Promise<void> | Promise used to return the execution result.| 606 607**Example** 608 609```js 610media.release() 611``` 612 613### storeMediaAsset 614 615storeMediaAsset(option: MediaAssetOption, callback: AsyncCallback<string>): void 616 617Stores a media asset. This API uses an asynchronous callback to return the URI that stores the media asset. 618 619> **NOTE** 620> 621> This API is supported since API version 6 and can be used only by the FA model. 622 623**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 624 625**Parameters** 626 627| Name | Type | Mandatory | Description | 628| -------- | ------------------------------------- | ---- | ----------------------- | 629| option | [MediaAssetOption](#mediaassetoption) | Yes | Media asset option. | 630| callback | AsyncCallback<string> | Yes | Callback used to return the URI that stores the media asset.| 631 632**Example** 633 634```js 635let option = { 636 src : '/data/storage/el2/base/haps/entry/image.png', 637 mimeType : 'image/*', 638 relativePath : 'Pictures/' 639}; 640mediaLibrary.getMediaLibrary().storeMediaAsset(option, (error, value) => { 641 if (error) { 642 console.error('storeMediaAsset failed with error: ' + error); 643 return; 644 } 645 console.info('Media resources stored. '); 646 // Obtain the URI that stores the media asset. 647}); 648``` 649 650 651### storeMediaAsset 652 653storeMediaAsset(option: MediaAssetOption): Promise<string> 654 655Stores a media asset. This API uses a promise to return the URI that stores the media asset. 656 657> **NOTE** 658> 659> This API is supported since API version 6 and can be used only by the FA model. 660 661**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 662 663**Parameters** 664 665| Name | Type | Mandatory | Description | 666| ------ | ------------------------------------- | ---- | ------- | 667| option | [MediaAssetOption](#mediaassetoption) | Yes | Media asset option.| 668 669**Return value** 670 671| Type | Description | 672| --------------------- | ---------------------------- | 673| Promise<string> | Promise used to return the URI that stores the media asset.| 674 675**Example** 676 677```js 678let option = { 679 src : '/data/storage/el2/base/haps/entry/image.png', 680 mimeType : 'image/*', 681 relativePath : 'Pictures/' 682}; 683mediaLibrary.getMediaLibrary().storeMediaAsset(option).then((value) => { 684 console.info('Media resources stored.'); 685 // Obtain the URI that stores the media asset. 686}).catch((error) => { 687 console.error('storeMediaAsset failed with error: ' + error); 688}); 689``` 690 691 692### startImagePreview 693 694startImagePreview(images: Array<string>, index: number, callback: AsyncCallback<void>): void 695 696Starts 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. 697 698> **NOTE** 699> This API is supported since API version 6 and can be used only by the FA model. 700> 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. 701 702**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 703 704**Parameters** 705 706| Name | Type | Mandatory | Description | 707| -------- | ------------------------- | ---- | ---------------------------------------- | 708| images | Array<string> | Yes | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.| 709| index | number | Yes | Index of the first image to preview. | 710| callback | AsyncCallback<void> | Yes | Callback used to return the image preview result. If the preview fails, an error message is returned. | 711 712**Example** 713 714```js 715let images = [ 716 'datashare:///media/xxxx/2', 717 'datashare:///media/xxxx/3' 718]; 719/* Preview online images. 720let images = [ 721 'https://media.xxxx.com/image1.jpg', 722 'https://media.xxxx.com/image2.jpg' 723]; 724*/ 725let index = 1; 726mediaLibrary.getMediaLibrary().startImagePreview(images, index, (error) => { 727 if (error) { 728 console.error('startImagePreview failed with error: ' + error); 729 return; 730 } 731 console.info('Succeeded in previewing the images.'); 732}); 733``` 734 735 736### startImagePreview 737 738startImagePreview(images: Array<string>, callback: AsyncCallback<void>): void 739 740Starts 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. 741 742> **NOTE** 743> This API is supported since API version 6 and can be used only by the FA model. 744> 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. 745 746**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 747 748**Parameters** 749 750| Name | Type | Mandatory | Description | 751| -------- | ------------------------- | ---- | ---------------------------------------- | 752| images | Array<string> | Yes | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.| 753| callback | AsyncCallback<void> | Yes | Callback used to return the image preview result. If the preview fails, an error message is returned. | 754 755**Example** 756 757```js 758let images = [ 759 'datashare:///media/xxxx/2', 760 'datashare:///media/xxxx/3' 761]; 762/* Preview online images. 763let images = [ 764 'https://media.xxxx.com/image1.jpg', 765 'https://media.xxxx.com/image2.jpg' 766]; 767*/ 768mediaLibrary.getMediaLibrary().startImagePreview(images, (error) => { 769 if (error) { 770 console.error('startImagePreview failed with error: ' + error); 771 return; 772 } 773 console.info('Succeeded in previewing the images.'); 774}); 775``` 776 777 778### startImagePreview 779 780startImagePreview(images: Array<string>, index?: number): Promise<void> 781 782Starts 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. 783 784> **NOTE** 785> This API is supported since API version 6 and can be used only by the FA model. 786> 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. 787 788**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 789 790**Parameters** 791 792| Name | Type | Mandatory | Description | 793| ------ | ------------------- | ---- | ---------------------------------------- | 794| images | Array<string> | Yes | URIs of the images to preview. The value can start with either **'https://'** or **'datashare://'**.| 795| index | number | No | Index of the first image to preview. If this parameter is not specified, the default value **0** is used. | 796 797**Return value** 798 799| Type | Description | 800| ------------------- | ------------------------------- | 801| Promise<void> | Promise used to return the image preview result. If the preview fails, an error message is returned.| 802 803**Example** 804 805```js 806let images = [ 807 'datashare:///media/xxxx/2', 808 'datashare:///media/xxxx/3' 809]; 810/* Preview online images. 811let images = [ 812 'https://media.xxxx.com/image1.jpg', 813 'https://media.xxxx.com/image2.jpg' 814]; 815*/ 816let index = 1; 817mediaLibrary.getMediaLibrary().startImagePreview(images, index).then(() => { 818 console.info('Succeeded in previewing the images.'); 819}).catch((error) => { 820 console.error('startImagePreview failed with error: ' + error); 821}); 822``` 823 824 825### startMediaSelect 826 827startMediaSelect(option: MediaSelectOption, callback: AsyncCallback<Array<string>>): void 828 829Starts media selection. This API uses an asynchronous callback to return the list of URIs that store the selected media assets. 830 831> **NOTE** 832> This API is supported since API version 6 and can be used only by the FA model. 833> 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). 834 835**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 836 837**Parameters** 838 839| Name | Type | Mandatory | Description | 840| -------- | ---------------------------------------- | ---- | ------------------------------------ | 841| option | [MediaSelectOption](#mediaselectoption) | Yes | Media selection option. | 842| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the list of URIs (starting with **datashare://**) that store the selected media assets.| 843 844**Example** 845 846```js 847let option : mediaLibrary.MediaSelectOption = { 848 type : 'media', 849 count : 2 850}; 851mediaLibrary.getMediaLibrary().startMediaSelect(option, (error, value) => { 852 if (error) { 853 console.error('startMediaSelect failed with error: ' + error); 854 return; 855 } 856 console.info('Media resources selected.'); 857 // Obtain the media selection value. 858}); 859``` 860 861 862### startMediaSelect 863 864startMediaSelect(option: MediaSelectOption): Promise<Array<string>> 865 866Starts media selection. This API uses a promise to return the list of URIs that store the selected media assets. 867 868> **NOTE** 869> This API is supported since API version 6 and can be used only by the FA model. 870> 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). 871 872**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 873 874**Parameters** 875 876| Name | Type | Mandatory | Description | 877| ------ | --------------------------------------- | ---- | ------- | 878| option | [MediaSelectOption](#mediaselectoption) | Yes | Media selection option.| 879 880**Return value** 881 882| Type | Description | 883| ---------------------------------- | ---------------------------------------- | 884| Promise<Array<string>> | Promise used to return the list of URIs (starting with **datashare://**) that store the selected media assets.| 885 886**Example** 887 888```js 889let option : mediaLibrary.MediaSelectOption = { 890 type : 'media', 891 count : 2 892}; 893mediaLibrary.getMediaLibrary().startMediaSelect(option).then((value) => { 894 console.info('Media resources selected.'); 895 // Obtain the media selection value. 896}).catch((error) => { 897 console.error('startMediaSelect failed with error: ' + error); 898}); 899 900``` 901### getActivePeers<sup>8+</sup> 902 903getActivePeers(): Promise\<Array\<PeerInfo>>; 904 905Obtains information about online peer devices. This API uses a promise to return the result. 906 907**System API**: This is a system API. 908 909**Required permissions**: ohos.permission.READ_MEDIA 910 911**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore 912 913**Return value** 914 915| Type | Description | 916| ------------------- | -------------------- | 917| Promise\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return the online peer devices, in an array of **PeerInfo** objects.| 918 919**Example** 920 921```js 922async function example() { 923 media.getActivePeers().then((devicesInfo) => { 924 if (devicesInfo != undefined) { 925 console.info('get distributed info ' + JSON.stringify(devicesInfo)); 926 } else { 927 console.info('get distributed info is undefined!'); 928 } 929 }).catch((error) => { 930 console.error('get distributed info failed with error: ' + error); 931 }); 932} 933``` 934 935### getActivePeers<sup>8+</sup> 936 937getActivePeers(callback: AsyncCallback\<Array\<PeerInfo>>): void; 938 939Obtains information about online peer devices. This API uses an asynchronous callback to return the result. 940 941**System API**: This is a system API. 942 943**Required permissions**: ohos.permission.READ_MEDIA 944 945**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore 946 947**Return value** 948 949| Type | Description | 950| ------------------- | -------------------- | 951| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return the online peer devices, in an array of **PeerInfo** objects.| 952 953**Example** 954 955```js 956async function example() { 957 media.getActivePeers((error, devicesInfo) => { 958 if (devicesInfo != undefined) { 959 console.info('get distributed info ' + JSON.stringify(devicesInfo)); 960 } else { 961 console.error('get distributed failed with error: ' + error); 962 } 963 }); 964} 965``` 966 967 968### getAllPeers<sup>8+</sup> 969 970getAllPeers(): Promise\<Array\<PeerInfo>>; 971 972Obtains information about all peer devices. This API uses a promise to return the result. 973 974**System API**: This is a system API. 975 976**Required permissions**: ohos.permission.READ_MEDIA 977 978**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore 979 980**Return value** 981 982| Type | Description | 983| ------------------- | -------------------- | 984| Promise\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return all peer devices, in an array of **PeerInfo** objects.| 985 986**Example** 987 988```js 989async function example() { 990 media.getAllPeers().then((devicesInfo) => { 991 if (devicesInfo != undefined) { 992 console.info('get distributed info ' + JSON.stringify(devicesInfo)); 993 } else { 994 console.info('get distributed info is undefined!'); 995 } 996 }).catch((error) => { 997 console.error('get distributed info failed with error: ' + error); 998 }); 999} 1000``` 1001 1002### getAllPeers<sup>8+</sup> 1003 1004getAllPeers(callback: AsyncCallback\<Array\<PeerInfo>>): void; 1005 1006Obtains information about online peer devices. This API uses an asynchronous callback to return the result. 1007 1008**System API**: This is a system API. 1009 1010**Required permissions**: ohos.permission.READ_MEDIA 1011 1012**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore 1013 1014**Return value** 1015 1016| Type | Description | 1017| ------------------- | -------------------- | 1018| callback: AsyncCallback\<Array\<[PeerInfo](#peerinfo8)>> | Promise used to return all peer devices, in an array of **PeerInfo** objects.| 1019 1020**Example** 1021 1022```js 1023async function example() { 1024 media.getAllPeers((error, devicesInfo) => { 1025 if (devicesInfo != undefined) { 1026 console.info('get distributed info ' + JSON.stringify(devicesInfo)); 1027 } else { 1028 console.error('get distributed failed with error: ' + error); 1029 } 1030 }); 1031} 1032``` 1033 1034## FileAsset<sup>7+</sup> 1035 1036Provides APIs for encapsulating file asset attributes. 1037 1038> **NOTE** 1039> 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. 1040> 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. 1041 1042### Attributes 1043 1044**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1045 1046| Name | Type | Readable| Writable| Description | 1047| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ | 1048| id | number | Yes | No | File asset ID. | 1049| uri | string | Yes | No | File asset URI, for example, **datashare:///media/image/2**. | 1050| mimeType | string | Yes | No | Extended file attributes. | 1051| mediaType<sup>8+</sup> | [MediaType](#mediatype8) | Yes | No | Media type. | 1052| displayName | string | Yes | Yes | Display file name, including the file name extension. | 1053| title | string | Yes | Yes | Title in the file. By default, it carries the file name without extension. | 1054| relativePath<sup>8+</sup> | string | Yes | Yes | Relative public directory of the file. | 1055| parent<sup>8+</sup> | number | Yes | No | Parent directory ID. | 1056| size | number | Yes | No | File size, in bytes. | 1057| dateAdded | number | Yes | No | Date when the file was added. The value is the number of seconds elapsed since the Epoch time. | 1058| 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.| 1059| dateTaken | number | Yes | No | Date when the file (photo) was taken. The value is the number of seconds elapsed since the Epoch time. | 1060| artist<sup>8+</sup> | string | Yes | No | Artist of the file. | 1061| audioAlbum<sup>8+</sup> | string | Yes | No | Audio album. | 1062| width | number | Yes | No | Image width, in pixels. | 1063| height | number | Yes | No | Image height, in pixels. | 1064| orientation | number | Yes | Yes | Image display direction (clockwise rotation angle, for example, 0, 90, or 180, in degrees).| 1065| duration<sup>8+</sup> | number | Yes | No | Duration, in ms. | 1066| albumId | number | Yes | No | ID of the album to which the file belongs. | 1067| albumUri<sup>8+</sup> | string | Yes | No | URI of the album to which the file belongs. | 1068| albumName | string | Yes | No | Name of the album to which the file belongs. | 1069 1070 1071### isDirectory<sup>8+</sup> 1072 1073isDirectory(callback: AsyncCallback<boolean>): void 1074 1075Checks whether this file asset is a directory. This API uses an asynchronous callback to return the result. 1076 1077**Required permissions**: ohos.permission.READ_MEDIA 1078 1079**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1080 1081**Parameters** 1082 1083| Name | Type | Mandatory | Description | 1084| -------- | ---------------------------- | ---- | ------------------- | 1085| callback | AsyncCallback<boolean> | Yes | Callback used to return whether the file asset is a directory.| 1086 1087**Example** 1088 1089```js 1090async function example() { 1091 let fileKeyObj = mediaLibrary.FileKey; 1092 let imageType = mediaLibrary.MediaType.IMAGE; 1093 let getImageOp = { 1094 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1095 selectionArgs: [imageType.toString()], 1096 order: fileKeyObj.DATE_ADDED + ' DESC', 1097 }; 1098 const fetchFileResult = await media.getFileAssets(getImageOp); 1099 const asset = await fetchFileResult.getFirstObject(); 1100 asset.isDirectory((error, isDirectory) => { 1101 if (error) { 1102 console.error('isDirectory failed with error: ' + error); 1103 } else { 1104 console.info('isDirectory result:' + isDirectory); 1105 } 1106 }); 1107 fetchFileResult.close(); 1108} 1109``` 1110 1111### isDirectory<sup>8+</sup> 1112 1113isDirectory():Promise<boolean> 1114 1115Checks whether this file asset is a directory. This API uses a promise to return the result. 1116 1117**Required permissions**: ohos.permission.READ_MEDIA 1118 1119**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1120 1121**Return value** 1122 1123| Type | Description | 1124| ---------------------- | ---------------------------- | 1125| Promise<boolean> | Promise used to return whether the file asset is a directory.| 1126 1127**Example** 1128 1129```js 1130async function example() { 1131 let fileKeyObj = mediaLibrary.FileKey; 1132 let imageType = mediaLibrary.MediaType.IMAGE; 1133 let getImageOp = { 1134 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1135 selectionArgs: [imageType.toString()], 1136 order: fileKeyObj.DATE_ADDED + ' DESC', 1137 }; 1138 const fetchFileResult = await media.getFileAssets(getImageOp); 1139 const asset = await fetchFileResult.getFirstObject(); 1140 asset.isDirectory().then((isDirectory) => { 1141 console.info('isDirectory result:' + isDirectory); 1142 }).catch((error) => { 1143 console.error('isDirectory failed with error: ' + error); 1144 }); 1145 fetchFileResult.close(); 1146} 1147``` 1148 1149### commitModify<sup>8+</sup> 1150 1151commitModify(callback: AsyncCallback<void>): void 1152 1153Commits the modification in this file asset to the database. This API uses an asynchronous callback to return the result. 1154 1155**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 1156 1157**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1158 1159**Parameters** 1160 1161| Name | Type | Mandatory | Description | 1162| -------- | ------------------------- | ---- | ----- | 1163| callback | AsyncCallback<void> | Yes | Void callback.| 1164 1165**Example** 1166 1167```js 1168async function example() { 1169 let fileKeyObj = mediaLibrary.FileKey; 1170 let imageType = mediaLibrary.MediaType.IMAGE; 1171 let getImageOp = { 1172 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1173 selectionArgs: [imageType.toString()], 1174 order: fileKeyObj.DATE_ADDED + ' DESC', 1175 }; 1176 const fetchFileResult = await media.getFileAssets(getImageOp); 1177 const asset = await fetchFileResult.getFirstObject(); 1178 asset.title = 'newtitle'; 1179 asset.commitModify(() => { 1180 console.info('commitModify successfully'); 1181 }); 1182 fetchFileResult.close(); 1183} 1184``` 1185 1186### commitModify<sup>8+</sup> 1187 1188commitModify(): Promise<void> 1189 1190Commits the modification in this file asset to the database. This API uses a promise to return the result. 1191 1192**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 1193 1194**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1195 1196**Return value** 1197 1198| Type | Description | 1199| ------------------- | ---------- | 1200| Promise<void> | Void promise.| 1201 1202**Example** 1203 1204```js 1205async function example() { 1206 let fileKeyObj = mediaLibrary.FileKey; 1207 let imageType = mediaLibrary.MediaType.IMAGE; 1208 let getImageOp = { 1209 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1210 selectionArgs: [imageType.toString()], 1211 order: fileKeyObj.DATE_ADDED + ' DESC', 1212 }; 1213 const fetchFileResult = await media.getFileAssets(getImageOp); 1214 const asset = await fetchFileResult.getFirstObject(); 1215 asset.title = 'newtitle'; 1216 await asset.commitModify(); 1217 fetchFileResult.close(); 1218} 1219``` 1220 1221### open<sup>8+</sup> 1222 1223open(mode: string, callback: AsyncCallback<number>): void 1224 1225Opens this file asset. This API uses an asynchronous callback to return the result. 1226 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. 1228 1229**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA 1230 1231**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1232 1233**Parameters** 1234 1235| Name | Type | Mandatory | Description | 1236| -------- | --------------------------- | ---- | ----------------------------------- | 1237| mode | string | Yes | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).| 1238| callback | AsyncCallback<number> | Yes | Callback used to return the file descriptor. | 1239 1240**Example** 1241 1242```js 1243async function example() { 1244 let mediaType = mediaLibrary.MediaType.IMAGE; 1245 let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; 1246 const path = await media.getPublicDirectory(DIR_IMAGE); 1247 const asset = await media.createAsset(mediaType, 'image00003.jpg', path); 1248 asset.open('rw', (error, fd) => { 1249 if (fd > 0) { 1250 asset.close(fd); 1251 } else { 1252 console.error('File Open failed with error: ' + error); 1253 } 1254 }); 1255} 1256``` 1257 1258### open<sup>8+</sup> 1259 1260open(mode: string): Promise<number> 1261 1262Opens this file asset. This API uses a promise to return the result. 1263 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. 1265 1266**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA 1267 1268**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1269 1270**Parameters** 1271 1272| Name | Type | Mandatory | Description | 1273| ---- | ------ | ---- | ----------------------------------- | 1274| mode | string | Yes | Mode of opening the file, for example, **'r'** (read-only), **'w'** (write-only), and **'rw'** (read-write).| 1275 1276**Return value** 1277 1278| Type | Description | 1279| --------------------- | ------------- | 1280| Promise<number> | Promise used to return the file descriptor.| 1281 1282**Example** 1283 1284```js 1285async function example() { 1286 let mediaType = mediaLibrary.MediaType.IMAGE; 1287 let DIR_IMAGE = mediaLibrary.DirectoryType.DIR_IMAGE; 1288 const path = await media.getPublicDirectory(DIR_IMAGE); 1289 const asset = await media.createAsset(mediaType, 'image00003.jpg', path); 1290 asset.open('rw').then((fd) => { 1291 console.info('File open fd: ' + fd); 1292 }).catch((error) => { 1293 console.error('File open failed with error: ' + error); 1294 }); 1295} 1296``` 1297 1298### close<sup>8+</sup> 1299 1300close(fd: number, callback: AsyncCallback<void>): void 1301 1302Closes this file asset. This API uses an asynchronous callback to return the result. 1303 1304**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA 1305 1306**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1307 1308**Parameters** 1309 1310| Name | Type | Mandatory | Description | 1311| -------- | ------------------------- | ---- | ----- | 1312| fd | number | Yes | File descriptor.| 1313| callback | AsyncCallback<void> | Yes | Void callback.| 1314 1315**Example** 1316 1317```js 1318async function example() { 1319 let fileKeyObj = mediaLibrary.FileKey; 1320 let imageType = mediaLibrary.MediaType.IMAGE; 1321 let getImageOp = { 1322 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1323 selectionArgs: [imageType.toString()], 1324 order: fileKeyObj.DATE_ADDED + ' DESC', 1325 }; 1326 const fetchFileResult = await media.getFileAssets(getImageOp); 1327 const asset = await fetchFileResult.getFirstObject(); 1328 asset.open('rw').then((fd) => { 1329 console.info('File open fd: ' + fd); 1330 asset.close(fd, (error) => { 1331 if (error) { 1332 console.error('asset.close failed with error: ' + error); 1333 } else { 1334 console.info('asset.close successfully'); 1335 } 1336 }); 1337 }).catch((error) => { 1338 console.error('File open failed with error: ' + error); 1339 }); 1340 fetchFileResult.close(); 1341} 1342``` 1343 1344### close<sup>8+</sup> 1345 1346close(fd: number): Promise<void> 1347 1348Closes this file asset. This API uses a promise to return the result. 1349 1350**Required permissions**: ohos.permission.READ_MEDIA or ohos.permission.WRITE_MEDIA 1351 1352**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1353 1354**Parameters** 1355 1356| Name | Type | Mandatory | Description | 1357| ---- | ------ | ---- | ----- | 1358| fd | number | Yes | File descriptor.| 1359 1360**Return value** 1361 1362| Type | Description | 1363| ------------------- | ---------- | 1364| Promise<void> | Void promise.| 1365 1366**Example** 1367 1368```js 1369async function example() { 1370 let fileKeyObj = mediaLibrary.FileKey; 1371 let imageType = mediaLibrary.MediaType.IMAGE; 1372 let getImageOp = { 1373 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1374 selectionArgs: [imageType.toString()], 1375 order: fileKeyObj.DATE_ADDED + ' DESC', 1376 }; 1377 const fetchFileResult = await media.getFileAssets(getImageOp); 1378 const asset = await fetchFileResult.getFirstObject(); 1379 asset.open('rw').then((fd) => { 1380 console.info('File fd!' + fd); 1381 asset.close(fd).then(() => { 1382 console.info('asset.close successfully'); 1383 }).catch((closeErr) => { 1384 console.error('asset.close fail, closeErr: ' + closeErr); 1385 }); 1386 }).catch((error) => { 1387 console.error('open File failed with error: ' + error); 1388 }); 1389 fetchFileResult.close(); 1390} 1391``` 1392 1393### getThumbnail<sup>8+</sup> 1394 1395getThumbnail(callback: AsyncCallback<image.PixelMap>): void 1396 1397Obtains the thumbnail of this file asset. This API uses an asynchronous callback to return the result. 1398 1399**Required permissions**: ohos.permission.READ_MEDIA 1400 1401**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1402 1403**Parameters** 1404 1405| Name | Type | Mandatory | Description | 1406| -------- | ----------------------------------- | ---- | ---------------- | 1407| callback | AsyncCallback<image.PixelMap> | Yes | Callback used to return the pixel map of the thumbnail.| 1408 1409**Example** 1410 1411```js 1412async function example() { 1413 let fileKeyObj = mediaLibrary.FileKey; 1414 let imageType = mediaLibrary.MediaType.IMAGE; 1415 let getImageOp = { 1416 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1417 selectionArgs: [imageType.toString()], 1418 order: fileKeyObj.DATE_ADDED + ' DESC', 1419 }; 1420 const fetchFileResult = await media.getFileAssets(getImageOp); 1421 const asset = await fetchFileResult.getFirstObject(); 1422 asset.getThumbnail((error, pixelmap) => { 1423 if (error) { 1424 console.error('mediaLibrary getThumbnail failed with error: ' + error); 1425 } else { 1426 console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap)); 1427 } 1428 }); 1429 fetchFileResult.close(); 1430} 1431``` 1432 1433### getThumbnail<sup>8+</sup> 1434 1435getThumbnail(size: Size, callback: AsyncCallback<image.PixelMap>): void 1436 1437Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses an asynchronous callback to return the result. 1438 1439**Required permissions**: ohos.permission.READ_MEDIA 1440 1441**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1442 1443**Parameters** 1444 1445| Name | Type | Mandatory | Description | 1446| -------- | ----------------------------------- | ---- | ---------------- | 1447| size | [Size](#size8) | Yes | Size of the thumbnail. | 1448| callback | AsyncCallback<image.PixelMap> | Yes | Callback used to return the pixel map of the thumbnail.| 1449 1450**Example** 1451 1452```js 1453async function example() { 1454 let fileKeyObj = mediaLibrary.FileKey; 1455 let imageType = mediaLibrary.MediaType.IMAGE; 1456 let getImageOp = { 1457 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1458 selectionArgs: [imageType.toString()], 1459 order: fileKeyObj.DATE_ADDED + ' DESC', 1460 }; 1461 let size = { width: 720, height: 720 }; 1462 const fetchFileResult = await media.getFileAssets(getImageOp); 1463 const asset = await fetchFileResult.getFirstObject(); 1464 asset.getThumbnail(size, (error, pixelmap) => { 1465 if (error) { 1466 console.error('mediaLibrary getThumbnail failed with error: ' + error); 1467 } else { 1468 console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap)); 1469 } 1470 }); 1471 fetchFileResult.close(); 1472} 1473``` 1474 1475### getThumbnail<sup>8+</sup> 1476 1477getThumbnail(size?: Size): Promise<image.PixelMap> 1478 1479Obtains the thumbnail of this file asset, with the thumbnail size passed. This API uses a promise to return the result. 1480 1481**Required permissions**: ohos.permission.READ_MEDIA 1482 1483**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1484 1485**Parameters** 1486 1487| Name | Type | Mandatory | Description | 1488| ---- | -------------- | ---- | ----- | 1489| size | [Size](#size8) | No | Size of the thumbnail.| 1490 1491**Return value** 1492 1493| Type | Description | 1494| ----------------------------- | --------------------- | 1495| Promise<image.PixelMap> | Promise to return the pixel map of the thumbnail.| 1496 1497**Example** 1498 1499```js 1500async function example() { 1501 let fileKeyObj = mediaLibrary.FileKey; 1502 let imageType = mediaLibrary.MediaType.IMAGE; 1503 let getImageOp = { 1504 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1505 selectionArgs: [imageType.toString()], 1506 order: fileKeyObj.DATE_ADDED + ' DESC', 1507 }; 1508 let size = { width: 720, height: 720 }; 1509 const fetchFileResult = await media.getFileAssets(getImageOp); 1510 const asset = await fetchFileResult.getFirstObject(); 1511 asset.getThumbnail(size).then((pixelmap) => { 1512 console.info('mediaLibrary getThumbnail Successful, pixelmap ' + JSON.stringify(pixelmap)); 1513 }).catch((error) => { 1514 console.error('mediaLibrary getThumbnail failed with error: ' + error); 1515 }); 1516 fetchFileResult.close(); 1517} 1518``` 1519 1520### favorite<sup>8+</sup> 1521 1522favorite(isFavorite: boolean, callback: AsyncCallback<void>): void 1523 1524Favorites or unfavorites this file asset. This API uses an asynchronous callback to return the result. 1525 1526**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 1527 1528**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1529 1530**Parameters** 1531 1532| Name | Type | Mandatory | Description | 1533| ---------- | ------------------------- | ---- | ---------------------------------- | 1534| 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.| 1535| callback | AsyncCallback<void> | Yes | Void callback. | 1536 1537**Example** 1538 1539```js 1540async function example() { 1541 let fileKeyObj = mediaLibrary.FileKey; 1542 let imageType = mediaLibrary.MediaType.IMAGE; 1543 let getImageOp = { 1544 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1545 selectionArgs: [imageType.toString()], 1546 order: fileKeyObj.DATE_ADDED + ' DESC', 1547 }; 1548 const fetchFileResult = await media.getFileAssets(getImageOp); 1549 const asset = await fetchFileResult.getFirstObject(); 1550 asset.favorite(true,(error) => { 1551 if (error) { 1552 console.error('mediaLibrary favorite failed with error: ' + error); 1553 } else { 1554 console.info('mediaLibrary favorite Successful'); 1555 } 1556 }); 1557 fetchFileResult.close(); 1558} 1559``` 1560 1561### favorite<sup>8+</sup> 1562 1563favorite(isFavorite: boolean): Promise<void> 1564 1565Favorites or unfavorites this file asset. This API uses a promise to return the result. 1566 1567**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 1568 1569**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1570 1571**Parameters** 1572 1573| Name | Type | Mandatory | Description | 1574| ---------- | ------- | ---- | ---------------------------------- | 1575| 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.| 1576 1577**Return value** 1578 1579| Type | Description | 1580| ------------------- | ---------- | 1581| Promise<void> | Void promise.| 1582 1583**Example** 1584 1585```js 1586async function example() { 1587 let fileKeyObj = mediaLibrary.FileKey; 1588 let imageType = mediaLibrary.MediaType.IMAGE; 1589 let getImageOp = { 1590 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1591 selectionArgs: [imageType.toString()], 1592 order: fileKeyObj.DATE_ADDED + ' DESC', 1593 }; 1594 const fetchFileResult = await media.getFileAssets(getImageOp); 1595 const asset = await fetchFileResult.getFirstObject(); 1596 asset.favorite(true).then(() => { 1597 console.info('mediaLibrary favorite Successful'); 1598 }).catch((error) => { 1599 console.error('mediaLibrary favorite failed with error: ' + error); 1600 }); 1601 fetchFileResult.close(); 1602} 1603``` 1604 1605### isFavorite<sup>8+</sup> 1606 1607isFavorite(callback: AsyncCallback<boolean>): void 1608 1609Checks whether this file asset is favorited. This API uses an asynchronous callback to return the result. 1610 1611**Required permissions**: ohos.permission.READ_MEDIA 1612 1613**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1614 1615**Parameters** 1616 1617| Name | Type | Mandatory | Description | 1618| -------- | ---------------------------- | ---- | ----------- | 1619| callback | AsyncCallback<boolean> | Yes | Callback used to return whether the file asset is favorited.| 1620 1621**Example** 1622 1623```js 1624async function example() { 1625 let fileKeyObj = mediaLibrary.FileKey; 1626 let imageType = mediaLibrary.MediaType.IMAGE; 1627 let getImageOp = { 1628 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1629 selectionArgs: [imageType.toString()], 1630 order: fileKeyObj.DATE_ADDED + ' DESC', 1631 }; 1632 const fetchFileResult = await media.getFileAssets(getImageOp); 1633 const asset = await fetchFileResult.getFirstObject(); 1634 asset.isFavorite((error, isFavorite) => { 1635 if (error) { 1636 console.error('mediaLibrary favoriisFavoritete failed with error: ' + error); 1637 } else { 1638 console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite); 1639 } 1640 }); 1641 fetchFileResult.close(); 1642} 1643``` 1644 1645### isFavorite<sup>8+</sup> 1646 1647isFavorite():Promise<boolean> 1648 1649Checks whether this file asset is favorited. This API uses a promise to return the result. 1650 1651**Required permissions**: ohos.permission.READ_MEDIA 1652 1653**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1654 1655**Return value** 1656 1657| Type | Description | 1658| ---------------------- | ------------------ | 1659| Promise<boolean> | Promise used to return whether the file asset is favorited.| 1660 1661**Example** 1662 1663```js 1664async function example() { 1665 let fileKeyObj = mediaLibrary.FileKey; 1666 let imageType = mediaLibrary.MediaType.IMAGE; 1667 let getImageOp = { 1668 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1669 selectionArgs: [imageType.toString()], 1670 order: fileKeyObj.DATE_ADDED + ' DESC', 1671 }; 1672 const fetchFileResult = await media.getFileAssets(getImageOp); 1673 const asset = await fetchFileResult.getFirstObject(); 1674 asset.isFavorite().then((isFavorite) => { 1675 console.info('mediaLibrary isFavorite Successful, isFavorite result: ' + isFavorite); 1676 }).catch((error) => { 1677 console.error('mediaLibrary favoriisFavoritete failed with error: ' + error); 1678 }); 1679 fetchFileResult.close(); 1680} 1681``` 1682 1683### trash<sup>8+</sup> 1684 1685trash(isTrash: boolean, callback: AsyncCallback<void>): void 1686 1687Moves this file asset to the trash. This API uses an asynchronous callback to return the result. 1688 1689Files in the trash are not actually deleted. You can set **isTrash** to **false** to restore the files from the trash. 1690 1691**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 1692 1693**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1694 1695**Parameters** 1696 1697| Name | Type | Mandatory | Description | 1698| -------- | ------------------------- | ---- | --------- | 1699| 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.| 1700| callback | AsyncCallback<void> | Yes | Void callback. | 1701 1702**Example** 1703 1704```js 1705async function example() { 1706 let fileKeyObj = mediaLibrary.FileKey; 1707 let imageType = mediaLibrary.MediaType.IMAGE; 1708 let getImageOp = { 1709 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1710 selectionArgs: [imageType.toString()], 1711 order: fileKeyObj.DATE_ADDED + ' DESC', 1712 }; 1713 const fetchFileResult = await media.getFileAssets(getImageOp); 1714 const asset = await fetchFileResult.getFirstObject(); 1715 asset.trash(true, (error) => { 1716 if (error) { 1717 console.error('mediaLibrary trash failed with error: ' + error); 1718 } else { 1719 console.info('mediaLibrary trash Successful'); 1720 } 1721 }); 1722 fetchFileResult.close(); 1723} 1724``` 1725 1726### trash<sup>8+</sup> 1727 1728trash(isTrash: boolean): Promise<void> 1729 1730Moves this file asset to the trash. This API uses a promise to return the result. 1731 1732Files in the trash are not actually deleted. You can set **isTrash** to **false** to restore the files from the trash. 1733 1734**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 1735 1736**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1737 1738**Parameters** 1739 1740| Name | Type | Mandatory | Description | 1741| ------- | ------- | ---- | --------- | 1742| 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.| 1743 1744**Return value** 1745 1746| Type | Description | 1747| ------------------- | ---------- | 1748| Promise<void> | Void promise.| 1749 1750**Example** 1751 1752```js 1753async function example() { 1754 let fileKeyObj = mediaLibrary.FileKey; 1755 let imageType = mediaLibrary.MediaType.IMAGE; 1756 let getImageOp = { 1757 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1758 selectionArgs: [imageType.toString()], 1759 order: fileKeyObj.DATE_ADDED + ' DESC', 1760 }; 1761 const fetchFileResult = await media.getFileAssets(getImageOp); 1762 const asset = await fetchFileResult.getFirstObject(); 1763 asset.trash(true).then(() => { 1764 console.info('trash successfully'); 1765 }).catch((error) => { 1766 console.error('trash failed with error: ' + error); 1767 }); 1768 fetchFileResult.close(); 1769} 1770``` 1771 1772### isTrash<sup>8+</sup> 1773 1774isTrash(callback: AsyncCallback<boolean>): void 1775 1776Checks whether this file asset is in the trash. This API uses an asynchronous callback to return the result. 1777 1778**Required permissions**: ohos.permission.READ_MEDIA 1779 1780**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1781 1782**Parameters** 1783 1784| Name | Type | Mandatory | Description | 1785| -------- | ---------------------------- | ---- | --------------- | 1786| callback | AsyncCallback<boolean> | 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.| 1787 1788**Example** 1789 1790```js 1791async function example() { 1792 let fileKeyObj = mediaLibrary.FileKey; 1793 let imageType = mediaLibrary.MediaType.IMAGE; 1794 let getImageOp = { 1795 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1796 selectionArgs: [imageType.toString()], 1797 order: fileKeyObj.DATE_ADDED + ' DESC', 1798 }; 1799 const fetchFileResult = await media.getFileAssets(getImageOp); 1800 const asset = await fetchFileResult.getFirstObject(); 1801 asset.isTrash((error, isTrash) => { 1802 if (error) { 1803 console.error('Failed to get trash state failed with error: ' + error); 1804 return; 1805 } 1806 console.info('Get trash state successfully, isTrash result: ' + isTrash); 1807 }); 1808 fetchFileResult.close(); 1809} 1810``` 1811 1812### isTrash<sup>8+</sup> 1813 1814isTrash():Promise<boolean> 1815 1816Checks whether this file asset is in the trash. This API uses a promise to return the result. 1817 1818**Required permissions**: ohos.permission.READ_MEDIA 1819 1820**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1821 1822**Return value** 1823 1824| Type | Description | 1825| ------------------- | -------------------- | 1826| Promise<void> | 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.| 1827 1828**Example** 1829 1830```js 1831async function example() { 1832 let fileKeyObj = mediaLibrary.FileKey; 1833 let imageType = mediaLibrary.MediaType.IMAGE; 1834 let getImageOp = { 1835 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1836 selectionArgs: [imageType.toString()], 1837 order: fileKeyObj.DATE_ADDED + ' DESC', 1838 }; 1839 const fetchFileResult = await media.getFileAssets(getImageOp); 1840 const asset = await fetchFileResult.getFirstObject(); 1841 asset.isTrash().then((isTrash) => { 1842 console.info('isTrash result: ' + isTrash); 1843 }).catch((error) => { 1844 console.error('isTrash failed with error: ' + error); 1845 }); 1846 fetchFileResult.close(); 1847} 1848``` 1849 1850## FetchFileResult<sup>7+</sup> 1851 1852Implements the result set of the file retrieval operation. 1853 1854### getCount<sup>7+</sup> 1855 1856getCount(): number 1857 1858Obtains the total number of files in the result set. 1859 1860**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1861 1862**Return value** 1863 1864| Type | Description | 1865| ------ | -------- | 1866| number | Total number of files.| 1867 1868**Example** 1869 1870```js 1871async function example() { 1872 let fileKeyObj = mediaLibrary.FileKey; 1873 let fileType = mediaLibrary.MediaType.FILE; 1874 let getFileCountOneOp = { 1875 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1876 selectionArgs: [fileType.toString()], 1877 order: fileKeyObj.DATE_ADDED + ' DESC', 1878 }; 1879 let fetchFileResult = await media.getFileAssets(getFileCountOneOp); 1880 const fetchCount = fetchFileResult.getCount(); 1881 console.info('fetchCount result: ' + fetchCount); 1882 fetchFileResult.close(); 1883} 1884``` 1885 1886### isAfterLast<sup>7+</sup> 1887 1888isAfterLast(): boolean 1889 1890Checks whether the cursor is in the last row of the result set. 1891 1892**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1893 1894**Return value** 1895 1896| Type | Description | 1897| ------- | ---------------------------------- | 1898| boolean | Returns **true** if the cursor is in the last row of the result set; returns *false* otherwise.| 1899 1900**Example** 1901 1902```js 1903async function example() { 1904 let fileKeyObj = mediaLibrary.FileKey; 1905 let imageType = mediaLibrary.MediaType.IMAGE; 1906 let getImageOp = { 1907 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1908 selectionArgs: [imageType.toString()], 1909 order: fileKeyObj.DATE_ADDED + ' DESC', 1910 }; 1911 let fetchFileResult = await media.getFileAssets(getImageOp); 1912 const fetchCount = fetchFileResult.getCount(); 1913 console.info('mediaLibrary fetchFileResult.getCount, count:' + fetchCount); 1914 let fileAsset = await fetchFileResult.getFirstObject(); 1915 for (var i = 1; i < fetchCount; i++) { 1916 fileAsset = await fetchFileResult.getNextObject(); 1917 if(i == fetchCount - 1) { 1918 var result = fetchFileResult.isAfterLast(); 1919 console.info('mediaLibrary fileAsset isAfterLast result: ' + result); 1920 fetchFileResult.close(); 1921 } 1922 } 1923} 1924``` 1925 1926### close<sup>7+</sup> 1927 1928close(): void 1929 1930Releases and invalidates this **FetchFileResult** instance. Other APIs in this instance cannot be invoked after it is released. 1931 1932**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1933 1934**Example** 1935 1936```js 1937async function example() { 1938 let fileKeyObj = mediaLibrary.FileKey; 1939 let imageType = mediaLibrary.MediaType.IMAGE; 1940 let getImageOp = { 1941 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1942 selectionArgs: [imageType.toString()], 1943 order: fileKeyObj.DATE_ADDED + ' DESC', 1944 }; 1945 let fetchFileResult = await media.getFileAssets(getImageOp); 1946 fetchFileResult.close(); 1947} 1948``` 1949 1950### getFirstObject<sup>7+</sup> 1951 1952getFirstObject(callback: AsyncCallback<FileAsset>): void 1953 1954Obtains the first file asset in the result set. This API uses an asynchronous callback to return the result. 1955 1956**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1957 1958**Parameters** 1959 1960| Name | Type | Mandatory| Description | 1961| -------- | --------------------------------------------- | ---- | ------------------------------------------- | 1962| callback | AsyncCallback<[FileAsset](#fileasset7)> | Yes | Callback used to return the first file asset.| 1963 1964**Example** 1965 1966```js 1967async function example() { 1968 let fileKeyObj = mediaLibrary.FileKey; 1969 let imageType = mediaLibrary.MediaType.IMAGE; 1970 let getImageOp = { 1971 selections: fileKeyObj.MEDIA_TYPE + '= ?', 1972 selectionArgs: [imageType.toString()], 1973 order: fileKeyObj.DATE_ADDED + ' DESC', 1974 }; 1975 let fetchFileResult = await media.getFileAssets(getImageOp); 1976 fetchFileResult.getFirstObject((error, fileAsset) => { 1977 if (error) { 1978 console.error('fetchFileResult getFirstObject failed with error: ' + error); 1979 return; 1980 } 1981 console.info('getFirstObject successfully, displayName : ' + fileAsset.displayName); 1982 fetchFileResult.close(); 1983 }) 1984} 1985``` 1986 1987### getFirstObject<sup>7+</sup> 1988 1989getFirstObject(): Promise<FileAsset> 1990 1991Obtains the first file asset in the result set. This API uses a promise to return the result. 1992 1993**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 1994 1995**Return value** 1996 1997| Type | Description | 1998| --------------------------------------- | -------------------------- | 1999| Promise<[FileAsset](#fileasset7)> | Promise used to return the file asset.| 2000 2001**Example** 2002 2003```js 2004async function example() { 2005 let fileKeyObj = mediaLibrary.FileKey; 2006 let imageType = mediaLibrary.MediaType.IMAGE; 2007 let getImageOp = { 2008 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2009 selectionArgs: [imageType.toString()], 2010 order: fileKeyObj.DATE_ADDED + ' DESC', 2011 }; 2012 let fetchFileResult = await media.getFileAssets(getImageOp); 2013 fetchFileResult.getFirstObject().then((fileAsset) => { 2014 console.info('getFirstObject successfully, displayName: ' + fileAsset.displayName); 2015 fetchFileResult.close(); 2016 }).catch((error) => { 2017 console.error('getFirstObject failed with error: ' + error); 2018 }); 2019} 2020``` 2021 2022### getNextObject<sup>7+</sup> 2023 2024 getNextObject(callback: AsyncCallback<FileAsset>): void 2025 2026Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result. 2027> **NOTE** 2028> 2029> 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. 2030 2031**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2032 2033**Parameters** 2034 2035| Name | Type | Mandatory| Description | 2036| --------- | --------------------------------------------- | ---- | ----------------------------------------- | 2037| callback| AsyncCallback<[FileAsset](#fileasset7)> | Yes | Callback used to return the next file asset.| 2038 2039**Example** 2040 2041```js 2042async function example() { 2043 let fileKeyObj = mediaLibrary.FileKey; 2044 let imageType = mediaLibrary.MediaType.IMAGE; 2045 let getImageOp = { 2046 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2047 selectionArgs: [imageType.toString()], 2048 order: fileKeyObj.DATE_ADDED + ' DESC', 2049 }; 2050 let fetchFileResult = await media.getFileAssets(getImageOp); 2051 let fileAsset = await fetchFileResult.getFirstObject(); 2052 if (!fileAsset.isAfterLast) { 2053 fetchFileResult.getNextObject((error, fileAsset) => { 2054 if (error) { 2055 console.error('fetchFileResult getNextObject failed with error: ' + error); 2056 return; 2057 } 2058 console.log('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName); 2059 fetchFileResult.close(); 2060 }) 2061 } 2062} 2063 2064``` 2065 2066### getNextObject<sup>7+</sup> 2067 2068 getNextObject(): Promise<FileAsset> 2069 2070Obtains the next file asset in the result set. This API uses a promise to return the result. 2071> **NOTE** 2072> 2073> 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. 2074 2075**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2076 2077**Return value** 2078 2079| Type | Description | 2080| --------------------------------------- | ----------------- | 2081| Promise<[FileAsset](#fileasset7)> | Promise used to return the next file asset.| 2082 2083**Example** 2084 2085```js 2086async function example() { 2087 let fileKeyObj = mediaLibrary.FileKey; 2088 let imageType = mediaLibrary.MediaType.IMAGE; 2089 let getImageOp = { 2090 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2091 selectionArgs: [imageType.toString()], 2092 order: fileKeyObj.DATE_ADDED + ' DESC', 2093 }; 2094 let fetchFileResult = await media.getFileAssets(getImageOp); 2095 let fileAsset = await fetchFileResult.getFirstObject(); 2096 if (!fileAsset.isAfterLast) { 2097 fetchFileResult.getNextObject().then((fileAsset) => { 2098 console.info('fetchFileResult getNextObject successfully, displayName: ' + fileAsset.displayName); 2099 fetchFileResult.close(); 2100 }).catch((error) => { 2101 console.error('fetchFileResult getNextObject failed with error: ' + error); 2102 }) 2103 } 2104} 2105``` 2106 2107### getLastObject<sup>7+</sup> 2108 2109getLastObject(callback: AsyncCallback<FileAsset>): void 2110 2111Obtains the last file asset in the result set. This API uses an asynchronous callback to return the result. 2112 2113**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2114 2115**Parameters** 2116 2117| Name | Type | Mandatory| Description | 2118| -------- | --------------------------------------------- | ---- | --------------------------- | 2119| callback | AsyncCallback<[FileAsset](#fileasset7)> | Yes | Callback used to return the last file asset.| 2120 2121**Example** 2122 2123```js 2124async function example() { 2125 let fileKeyObj = mediaLibrary.FileKey; 2126 let imageType = mediaLibrary.MediaType.IMAGE; 2127 let getImageOp = { 2128 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2129 selectionArgs: [imageType.toString()], 2130 order: fileKeyObj.DATE_ADDED + ' DESC', 2131 }; 2132 let fetchFileResult = await media.getFileAssets(getImageOp); 2133 fetchFileResult.getLastObject((error, fileAsset) => { 2134 if (error) { 2135 console.error('getLastObject failed with error: ' + error); 2136 return; 2137 } 2138 console.info('getLastObject successfully, displayName: ' + fileAsset.displayName); 2139 fetchFileResult.close(); 2140 }) 2141} 2142``` 2143 2144### getLastObject<sup>7+</sup> 2145 2146getLastObject(): Promise<FileAsset> 2147 2148Obtains the last file asset in the result set. This API uses a promise to return the result. 2149 2150**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2151 2152**Return value** 2153 2154| Type | Description | 2155| --------------------------------------- | ----------------- | 2156| Promise<[FileAsset](#fileasset7)> | Promise used to return the next file asset.| 2157 2158**Example** 2159 2160```js 2161async function example() { 2162 let fileKeyObj = mediaLibrary.FileKey; 2163 let imageType = mediaLibrary.MediaType.IMAGE; 2164 let getImageOp = { 2165 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2166 selectionArgs: [imageType.toString()], 2167 order: fileKeyObj.DATE_ADDED + ' DESC', 2168 }; 2169 let fetchFileResult = await media.getFileAssets(getImageOp); 2170 fetchFileResult.getLastObject().then((fileAsset) => { 2171 console.info('getLastObject successfully, displayName: ' + fileAsset.displayName); 2172 fetchFileResult.close(); 2173 }).catch((error) => { 2174 console.error('getLastObject failed with error: ' + error); 2175 }); 2176} 2177``` 2178 2179### getPositionObject<sup>7+</sup> 2180 2181getPositionObject(index: number, callback: AsyncCallback<FileAsset>): void 2182 2183Obtains a file asset with the specified index in the result set. This API uses an asynchronous callback to return the result. 2184 2185**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2186 2187**Parameters** 2188 2189| Name | Type | Mandatory | Description | 2190| -------- | ---------------------------------------- | ---- | ------------------ | 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. | 2192| callback | AsyncCallback<[FileAsset](#fileasset7)> | Yes | Callback used to return the last file asset.| 2193 2194**Example** 2195 2196```js 2197async function example() { 2198 let fileKeyObj = mediaLibrary.FileKey; 2199 let imageType = mediaLibrary.MediaType.IMAGE; 2200 let getImageOp = { 2201 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2202 selectionArgs: [imageType.toString()], 2203 order: fileKeyObj.DATE_ADDED + ' DESC', 2204 }; 2205 let fetchFileResult = await media.getFileAssets(getImageOp); 2206 fetchFileResult.getPositionObject(0, (error, fileAsset) => { 2207 if (error) { 2208 console.error('getPositionObject failed with error: ' + error); 2209 return; 2210 } 2211 console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName); 2212 fetchFileResult.close(); 2213 }) 2214} 2215``` 2216 2217### getPositionObject<sup>7+</sup> 2218 2219getPositionObject(index: number): Promise<FileAsset> 2220 2221Obtains a file asset with the specified index in the result set. This API uses a promise to return the result. 2222 2223**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2224 2225**Parameters** 2226 2227| Name | Type | Mandatory | Description | 2228| ----- | ------ | ---- | -------------- | 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.| 2230 2231**Return value** 2232 2233| Type | Description | 2234| --------------------------------------- | ----------------- | 2235| Promise<[FileAsset](#fileasset7)> | Promise used to return the next file asset.| 2236 2237**Example** 2238 2239```js 2240async function example() { 2241 let fileKeyObj = mediaLibrary.FileKey; 2242 let imageType = mediaLibrary.MediaType.IMAGE; 2243 let getImageOp = { 2244 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2245 selectionArgs: [imageType.toString()], 2246 order: fileKeyObj.DATE_ADDED + ' DESC', 2247 }; 2248 let fetchFileResult = await media.getFileAssets(getImageOp); 2249 fetchFileResult.getPositionObject(0).then((fileAsset) => { 2250 console.info('getPositionObject successfully, displayName: ' + fileAsset.displayName); 2251 fetchFileResult.close(); 2252 }).catch((error) => { 2253 console.error('getPositionObject failed with error: ' + error); 2254 }); 2255} 2256``` 2257 2258### getAllObject<sup>7+</sup> 2259 2260getAllObject(callback: AsyncCallback<Array<FileAsset>>): void 2261 2262Obtains all the file assets in the result set. This API uses an asynchronous callback to return the result. 2263 2264**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2265 2266**Parameters** 2267 2268| Name | Type | Mandatory | Description | 2269| -------- | ---------------------------------------- | ---- | -------------------- | 2270| callback | AsyncCallback<Array<[FileAsset](#fileasset7)>> | Yes | Callback used to return the file assets.| 2271 2272**Example** 2273 2274```js 2275async function example() { 2276 let fileKeyObj = mediaLibrary.FileKey; 2277 let imageType = mediaLibrary.MediaType.IMAGE; 2278 let getImageOp = { 2279 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2280 selectionArgs: [imageType.toString()], 2281 order: fileKeyObj.DATE_ADDED + ' DESC', 2282 }; 2283 let fetchFileResult = await media.getFileAssets(getImageOp); 2284 fetchFileResult.getAllObject((error, fileAssetList) => { 2285 if (error) { 2286 console.error('getAllObject failed with error: ' + error); 2287 return; 2288 } 2289 for (let i = 0; i < fetchFileResult.getCount(); i++) { 2290 console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName); 2291 } 2292 fetchFileResult.close(); 2293 }) 2294} 2295``` 2296 2297### getAllObject<sup>7+</sup> 2298 2299getAllObject(): Promise<Array<FileAsset>> 2300 2301Obtains all the file assets in the result set. This API uses a promise to return the result. 2302 2303**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2304 2305**Return value** 2306 2307| Type | Description | 2308| ---------------------------------------- | --------------------- | 2309| Promise<Array<[FileAsset](#fileasset7)>> | Promise used to return the file assets.| 2310 2311**Example** 2312 2313```js 2314async function example() { 2315 let fileKeyObj = mediaLibrary.FileKey; 2316 let imageType = mediaLibrary.MediaType.IMAGE; 2317 let getImageOp = { 2318 selections: fileKeyObj.MEDIA_TYPE + '= ?', 2319 selectionArgs: [imageType.toString()], 2320 order: fileKeyObj.DATE_ADDED + ' DESC', 2321 }; 2322 let fetchFileResult = await media.getFileAssets(getImageOp); 2323 fetchFileResult.getAllObject().then((fileAssetList) => { 2324 for (let i = 0; i < fetchFileResult.getCount(); i++) { 2325 console.info('getAllObject fileAssetList ' + i + ' displayName: ' + fileAssetList[i].displayName); 2326 } 2327 fetchFileResult.close(); 2328 }).catch((error) => { 2329 console.error('getAllObject failed with error: ' + error); 2330 }); 2331} 2332``` 2333 2334## Album<sup>7+</sup> 2335 2336Provides APIs to implement a physical album. 2337 2338### Attributes 2339 2340**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2341 2342| Name | Type | Readable | Writable | Description | 2343| ------------ | ------ | ---- | ---- | ------- | 2344| albumId | number | Yes | No | Album ID. | 2345| albumName | string | Yes | Yes | Album name. | 2346| albumUri<sup>8+</sup> | string | Yes | No | Album URI. | 2347| dateModified | number | Yes | No | Date when the album was modified. | 2348| count<sup>8+</sup> | number | Yes | No | Number of files in the album.| 2349| relativePath<sup>8+</sup> | string | Yes | No | Relative path of the album. | 2350| coverUri<sup>8+</sup> | string | Yes | No | URI of the cover file of the album.| 2351 2352### commitModify<sup>8+</sup> 2353 2354commitModify(callback: AsyncCallback<void>): void 2355 2356Commits the modification in the album attributes to the database. This API uses an asynchronous callback to return the result. 2357 2358**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 2359 2360**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2361 2362**Parameters** 2363 2364| Name | Type | Mandatory| Description | 2365| -------- | ------------------------- | ---- | ---------- | 2366| callback | AsyncCallback<void> | Yes | Void callback.| 2367 2368**Example** 2369 2370```js 2371async function example() { 2372 let AlbumNoArgsfetchOp = { 2373 selections: '', 2374 selectionArgs: [], 2375 }; 2376 const albumList = await media.getAlbums(AlbumNoArgsfetchOp); 2377 const album = albumList[0]; 2378 album.albumName = 'hello'; 2379 album.commitModify((error) => { 2380 if (error) { 2381 console.error('commitModify failed with error: ' + error); 2382 return; 2383 } 2384 console.info('commitModify successful.'); 2385 }) 2386} 2387``` 2388 2389### commitModify<sup>8+</sup> 2390 2391commitModify(): Promise<void> 2392 2393Commits the modification in the album attributes to the database. This API uses a promise to return the result. 2394 2395**Required permissions**: ohos.permission.READ_MEDIA and ohos.permission.WRITE_MEDIA 2396 2397**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2398 2399**Return value** 2400 2401| Type | Description | 2402| ------------------- | ------------ | 2403| Promise<void> | Void promise.| 2404 2405**Example** 2406 2407```js 2408async function example() { 2409 let AlbumNoArgsfetchOp = { 2410 selections: '', 2411 selectionArgs: [], 2412 }; 2413 const albumList = await media.getAlbums(AlbumNoArgsfetchOp); 2414 const album = albumList[0]; 2415 album.albumName = 'hello'; 2416 album.commitModify().then(() => { 2417 console.info('commitModify successfully'); 2418 }).catch((error) => { 2419 console.error('commitModify failed with error: ' + error); 2420 }); 2421} 2422``` 2423 2424### getFileAssets<sup>7+</sup> 2425 2426getFileAssets(options: MediaFetchOptions, callback: AsyncCallback<FetchFileResult>): void 2427 2428Obtains the file assets in this album. This API uses an asynchronous callback to return the result. 2429 2430**Required permissions**: ohos.permission.READ_MEDIA 2431 2432**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2433 2434**Parameters** 2435 2436| Name | Type | Mandatory| Description | 2437| -------- | --------------------------------------------------- | ---- | ----------------------------------- | 2438| options | [MediaFetchOptions](#mediafetchoptions7) | Yes | Options for fetching the files. | 2439| callback | AsyncCallback<[FetchFileResult](#fetchfileresult7)> | Yes | Callback used to return the file assets.| 2440 2441**Example** 2442 2443```js 2444async function example() { 2445 let AlbumNoArgsfetchOp = { 2446 selections: '', 2447 selectionArgs: [], 2448 }; 2449 let fileNoArgsfetchOp = { 2450 selections: '', 2451 selectionArgs: [], 2452 } 2453 // Obtain the albums that meet the retrieval options and return the album list. 2454 const albumList = await media.getAlbums(AlbumNoArgsfetchOp); 2455 const album = albumList[0]; 2456 // Obtain an album from the album list and obtain all media assets that meet the retrieval options in the album. 2457 album.getFileAssets(fileNoArgsfetchOp, (error, fetchFileResult) => { 2458 if (error) { 2459 console.error('album getFileAssets failed with error: ' + error); 2460 return; 2461 } 2462 let count = fetchFileResult.getCount(); 2463 console.info('album getFileAssets successfully, count: ' + count); 2464 fetchFileResult.close(); 2465 }); 2466} 2467``` 2468 2469### getFileAssets<sup>7+</sup> 2470 2471 getFileAssets(options?: MediaFetchOptions): Promise<FetchFileResult> 2472 2473Obtains the file assets in this album. This API uses a promise to return the result. 2474 2475**Required permissions**: ohos.permission.READ_MEDIA 2476 2477**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2478 2479**Parameters** 2480 2481| Name | Type | Mandatory| Description | 2482| ------- | ---------------------------------------- | ---- | -------------- | 2483| options | [MediaFetchOptions](#mediafetchoptions7) | No | Options for fetching the files.| 2484 2485**Return value** 2486 2487| Type | Description | 2488| --------------------------------------------- | ------------------------- | 2489| Promise<[FetchFileResult](#fetchfileresult7)> | Promise used to return the file assets.| 2490 2491**Example** 2492 2493```js 2494async function example() { 2495 let AlbumNoArgsfetchOp = { 2496 selections: '', 2497 selectionArgs: [], 2498 }; 2499 let fileNoArgsfetchOp = { 2500 selections: '', 2501 selectionArgs: [], 2502 }; 2503 // Obtain the albums that meet the retrieval options and return the album list. 2504 const albumList = await media.getAlbums(AlbumNoArgsfetchOp); 2505 const album = albumList[0]; 2506 // Obtain an album from the album list and obtain all media assets that meet the retrieval options in the album. 2507 album.getFileAssets(fileNoArgsfetchOp).then((fetchFileResult) => { 2508 let count = fetchFileResult.getCount(); 2509 console.info('album getFileAssets successfully, count: ' + count); 2510 fetchFileResult.close(); 2511 }).catch((error) => { 2512 console.error('album getFileAssets failed with error: ' + error); 2513 }); 2514} 2515``` 2516 2517## PeerInfo<sup>8+</sup> 2518 2519Describes information about a registered device. 2520 2521**System API**: This is a system API. 2522 2523**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore 2524 2525| Name | Type | Readable| Writable| Description | 2526| ---------- | -------------------------- | ---- | ---- | ---------------- | 2527| deviceName | string | Yes | No | Name of the registered device. | 2528| networkId | string | Yes | No | Network ID of the registered device.| 2529| deviceType | [DeviceType](#devicetype8) | Yes | No | Type of the registered device. | 2530| isOnline | boolean | Yes | No | Whether the registered device is online. | 2531 2532 2533 2534## MediaType<sup>8+</sup> 2535 2536Enumerates media types. 2537 2538**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2539 2540| Name | Value| Description| 2541| ----- | ---- | ---- | 2542| FILE | 0 | File.| 2543| IMAGE | 1 | Image.| 2544| VIDEO | 2 | Video.| 2545| AUDIO | 3 | Audio.| 2546 2547## FileKey<sup>8+</sup> 2548 2549Enumerates key file information. 2550 2551> **NOTE** 2552> The **bucket_id** field may change after file rename or movement. Therefore, you must obtain the field again before using it. 2553 2554**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2555 2556| Name | Value | Description | 2557| ------------- | ------------------- | ---------------------------------------------------------- | 2558| ID | 'file_id' | File ID. | 2559| RELATIVE_PATH | 'relative_path' | Relative public directory of the file. | 2560| DISPLAY_NAME | 'display_name' | Display file name. | 2561| PARENT | 'parent' | Parent directory ID. | 2562| MIME_TYPE | 'mime_type' | Extended file attributes, such as image/, video/, and file/*. | 2563| MEDIA_TYPE | 'media_type' | Media type. | 2564| SIZE | 'size' | File size, in bytes. | 2565| DATE_ADDED | 'date_added' | Date when the file was added. The value is the number of seconds elapsed since the Epoch time. | 2566| 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.| 2567| DATE_TAKEN | 'date_taken' | Date when the file (photo) was taken. The value is the number of seconds elapsed since the Epoch time. | 2568| TITLE | 'title' | Title in the file. | 2569| ARTIST | 'artist' | Artist of the file. | 2570| AUDIOALBUM | 'audio_album' | Audio album. | 2571| DURATION | 'duration' | Duration, in ms. | 2572| WIDTH | 'width' | Image width, in pixels. | 2573| HEIGHT | 'height' | Image height, in pixels. | 2574| ORIENTATION | 'orientation' | Image display direction (clockwise rotation angle, for example, 0, 90, and 180, in degrees).| 2575| ALBUM_ID | 'bucket_id' | ID of the album to which the file belongs. | 2576| ALBUM_NAME | 'bucket_display_name' | Name of the album to which the file belongs. | 2577 2578## DirectoryType<sup>8+</sup> 2579 2580Enumerates directory types. 2581 2582**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2583 2584| Name | Value| Description | 2585| ------------- | --- | ------------------ | 2586| DIR_CAMERA | 0 | Directory of camera files.| 2587| DIR_VIDEO | 1 | Directory of video files. | 2588| DIR_IMAGE | 2 | Directory of image files. | 2589| DIR_AUDIO | 3 | Directory of audio files. | 2590| DIR_DOCUMENTS | 4 | Directory of documents. | 2591| DIR_DOWNLOAD | 5 | Download directory. | 2592 2593## DeviceType<sup>8+</sup> 2594 2595Enumerates device types. 2596 2597**System API**: This is a system API. 2598 2599**System capability**: SystemCapability.Multimedia.MediaLibrary.DistributedCore 2600 2601| Name | Value| Description | 2602| ------------ | --- | ---------- | 2603| TYPE_UNKNOWN | 0 | Unknown.| 2604| TYPE_LAPTOP | 1 | Laptop.| 2605| TYPE_PHONE | 2 | Phone. | 2606| TYPE_TABLET | 3 | Tablet. | 2607| TYPE_WATCH | 4 | Smart watch. | 2608| TYPE_CAR | 5 | Vehicle-mounted device. | 2609| TYPE_TV | 6 | TV. | 2610 2611## MediaFetchOptions<sup>7+</sup> 2612 2613Describes options for fetching media files. 2614 2615**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2616 2617| Name | Type | Readable| Writable| Description | 2618| ----------------------- | ------------------- | ---- | ---- | ------------------------------------------------------------ | 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<string> | 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()], | 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| uri<sup>8+</sup> | string | Yes | Yes | File URI. | 2623| networkId<sup>8+</sup> | string | Yes | Yes | Network ID of the registered device. | 2624| extendArgs<sup>8+</sup> | string | Yes | Yes | Extended parameters for fetching the files. Currently, no extended parameters are available. | 2625 2626## Size<sup>8+</sup> 2627 2628Describes the image size. 2629 2630**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2631 2632| Name | Type | Readable | Writable | Description | 2633| ------ | ------ | ---- | ---- | -------- | 2634| width | number | Yes | Yes | Image width, in pixels.| 2635| height | number | Yes | Yes | Image height, in pixels.| 2636 2637## MediaAssetOption 2638 2639Implements the media asset option. 2640 2641**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2642 2643 2644| Name | Type | Readable| Writable| Description | 2645| ------------ | ------ | ---- | ---- | ------------------------------------------------------------ | 2646| src | string | Yes | Yes | Application sandbox oath of the local file. | 2647| mimeType | string | Yes | Yes | Multipurpose Internet Mail Extensions (MIME) type of the media.<br>The value can be 'image/\*', 'video/\*', 'audio/\*' or 'file\*'.| 2648| 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/'| 2649 2650## MediaSelectOption 2651 2652Describes media selection option. 2653 2654**System capability**: SystemCapability.Multimedia.MediaLibrary.Core 2655 2656| Name | Type | Readable| Writable| Description | 2657| ----- | ------ | ---- | ---- | -------------------- | 2658| type | 'image' | 'video' | 'media' | Yes | Yes | Media type, which can be **image**, **media**, or **video**. Currently, only **media** is supported.| 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. | 2660