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