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