1# @ohos.file.photoAccessHelper (Album Management) 2 3The **photoAccessHelper** module provides APIs for album management, including creating an album and accessing and modifying media data in an album. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```ts 12import photoAccessHelper from '@ohos.file.photoAccessHelper'; 13``` 14 15## photoAccessHelper.getPhotoAccessHelper 16 17getPhotoAccessHelper(context: Context): PhotoAccessHelper 18 19Obtains a **PhotoAccessHelper** instance for accessing and modifying media files in the album. 20 21**Model restriction**: This API can be used only in the stage model. 22 23**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 24 25**Parameters** 26 27| Name | Type | Mandatory| Description | 28| ------- | ------- | ---- | -------------------------- | 29| context | [Context](js-apis-inner-application-context.md) | Yes | Context of the ability instance.| 30 31**Return value** 32 33| Type | Description | 34| ----------------------------- | :---- | 35| [PhotoAccessHelper](#photoaccesshelper) | **PhotoAccessHelper** instance obtained.| 36 37**Error codes** 38 39For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md). 40 41| ID| Error Message| 42| -------- | ---------------------------------------- | 43| 401 | if parameter is invalid. | 44 45**Example** 46 47```ts 48// The phAccessHelper 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 phAccessHelper is not defined. 49let context = getContext(this); 50let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 51``` 52 53## PhotoAccessHelper 54 55### getAssets 56 57getAssets(options: FetchOptions, callback: AsyncCallback<FetchResult<PhotoAsset>>): void 58 59Obtains image and video assets. This API uses an asynchronous callback to return the result. 60 61**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 62 63**Required permissions**: ohos.permission.READ_IMAGEVIDEO 64 65**Parameters** 66 67| Name | Type | Mandatory| Description | 68| -------- | ------------------------ | ---- | ------------------------- | 69| options | [FetchOptions](#fetchoptions) | Yes | Options for fetching the image and video assets. | 70| callback | AsyncCallback<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Yes | Callback invoked to return the image and video assets obtained.| 71 72**Error codes** 73 74For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 75 76| ID| Error Message| 77| -------- | ---------------------------------------- | 78| 401 | if parameter is invalid. | 79| 13900012 | Permission denied. | 80| 13900020 | Invalid argument. | 81| 14000011 | System inner fail. | 82 83**Example** 84 85```ts 86import dataSharePredicates from '@ohos.data.dataSharePredicates'; 87 88async function example() { 89 console.info('getAssets'); 90 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 91 let fetchOptions: photoAccessHelper.FetchOptions = { 92 fetchColumns: [], 93 predicates: predicates 94 }; 95 96 phAccessHelper.getAssets(fetchOptions, async (err, fetchResult) => { 97 if (fetchResult !== undefined) { 98 console.info('fetchResult success'); 99 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 100 if (photoAsset !== undefined) { 101 console.info('photoAsset.displayName : ' + photoAsset.displayName); 102 } 103 } else { 104 console.error('fetchResult fail' + err); 105 } 106 }); 107} 108``` 109 110### getAssets 111 112getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 113 114Obtains image and video assets. This API uses a promise to return the result. 115 116**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 117 118**Required permissions**: ohos.permission.READ_IMAGEVIDEO 119 120**Parameters** 121 122| Name | Type | Mandatory| Description | 123| ------- | ------------------- | ---- | ---------------- | 124| options | [FetchOptions](#fetchoptions) | Yes | Options for fetching the image and video assets. | 125 126**Return value** 127 128| Type | Description | 129| --------------------------- | -------------- | 130| Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise used to return the image and video assets obtained.| 131 132**Error codes** 133 134For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 135 136| ID| Error Message| 137| -------- | ---------------------------------------- | 138| 401 | if parameter is invalid. | 139| 13900012 | Permission denied. | 140| 13900020 | Invalid argument. | 141| 14000011 | System inner fail. | 142 143**Example** 144 145```ts 146import dataSharePredicates from '@ohos.data.dataSharePredicates'; 147 148async function example() { 149 console.info('getAssets'); 150 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 151 let fetchOptions: photoAccessHelper.FetchOptions = { 152 fetchColumns: [], 153 predicates: predicates 154 }; 155 try { 156 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 157 if (fetchResult !== undefined) { 158 console.info('fetchResult success'); 159 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 160 if (photoAsset !== undefined) { 161 console.info('photoAsset.displayName :' + photoAsset.displayName); 162 } 163 } 164 } catch (err) { 165 console.error('getAssets failed, message = ', err); 166 } 167} 168``` 169 170### createAsset 171 172createAsset(displayName: string, callback: AsyncCallback<PhotoAsset>): void 173 174Creates an image or video asset with the specified file name. This API uses an asynchronous callback to return the result. 175 176**System API**: This is a system API. 177 178**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 179 180**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 181 182**Parameters** 183 184| Name | Type | Mandatory| Description | 185| -------- | ------------------------ | ---- | ------------------------- | 186| displayName | string | Yes | File name of the image or video to create. | 187| callback | AsyncCallback<[PhotoAsset](#photoasset)> | Yes | Callback invoked to return the image or video created.| 188 189**Error codes** 190 191For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 192 193| ID| Error Message| 194| -------- | ---------------------------------------- | 195| 202 | Called by non-system application. | 196| 401 | if parameter is invalid. | 197| 13900012 | Permission denied. | 198| 13900020 | Invalid argument. | 199| 14000001 | Invalid display name. | 200| 14000011 | System inner fail. | 201 202**Example** 203 204```ts 205async function example() { 206 console.info('createAssetDemo'); 207 let testFileName: string = 'testFile' + Date.now() + '.jpg'; 208 phAccessHelper.createAsset(testFileName, (err, photoAsset) => { 209 if (photoAsset !== undefined) { 210 console.info('createAsset file displayName' + photoAsset.displayName); 211 console.info('createAsset successfully'); 212 } else { 213 console.error('createAsset failed, message = ', err); 214 } 215 }); 216} 217``` 218 219### createAsset 220 221createAsset(displayName: string): Promise<PhotoAsset> 222 223Creates an image or video asset with the specified file name. This API uses a promise to return the result. 224 225**System API**: This is a system API. 226 227**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.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 237**Return value** 238 239| Type | Description | 240| --------------------------- | -------------- | 241| Promise<[PhotoAsset](#photoasset)> | Promise used to return the created image and video asset.| 242 243**Error codes** 244 245For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 246 247| ID| Error Message| 248| -------- | ---------------------------------------- | 249| 202 | Called by non-system application. | 250| 401 | if parameter is invalid. | 251| 13900012 | Permission denied. | 252| 13900020 | Invalid argument. | 253| 14000001 | Invalid display name. | 254| 14000011 | System inner fail. | 255 256**Example** 257 258```ts 259async function example() { 260 console.info('createAssetDemo'); 261 try { 262 let testFileName: string = 'testFile' + Date.now() + '.jpg'; 263 let photoAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(testFileName); 264 console.info('createAsset file displayName' + photoAsset.displayName); 265 console.info('createAsset successfully'); 266 } catch (err) { 267 console.error('createAsset failed, message = ', err); 268 } 269} 270``` 271 272### createAsset 273 274createAsset(displayName: string, options: PhotoCreateOptions, callback: AsyncCallback<PhotoAsset>): void 275 276Creates an image or video asset with the specified file name and options. This API uses an asynchronous callback to return the result. 277 278**System API**: This is a system API. 279 280**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 281 282**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 283 284**Parameters** 285 286| Name | Type | Mandatory| Description | 287| -------- | ------------------------ | ---- | ------------------------- | 288| displayName | string | Yes | File name of the image or video to create. | 289| options | [PhotoCreateOptions](#photocreateoptions) | Yes | Options for creating an image or video asset. | 290| callback | AsyncCallback<[PhotoAsset](#photoasset)> | Yes | Callback invoked to return the image or video created.| 291 292**Error codes** 293 294For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 295 296| ID| Error Message| 297| -------- | ---------------------------------------- | 298| 202 | Called by non-system application. | 299| 401 | if parameter is invalid. | 300| 13900012 | Permission denied. | 301| 13900020 | Invalid argument. | 302| 14000001 | Invalid display name. | 303| 14000011 | System inner fail. | 304 305**Example** 306 307```ts 308async function example() { 309 console.info('createAssetDemo'); 310 let testFileName: string = 'testFile' + Date.now() + '.jpg'; 311 let createOption: photoAccessHelper.PhotoCreateOptions = { 312 subtype: photoAccessHelper.PhotoSubtype.DEFAULT 313 } 314 phAccessHelper.createAsset(testFileName, createOption, (err, photoAsset) => { 315 if (photoAsset !== undefined) { 316 console.info('createAsset file displayName' + photoAsset.displayName); 317 console.info('createAsset successfully'); 318 } else { 319 console.error('createAsset failed, message = ', err); 320 } 321 }); 322} 323``` 324 325### createAsset 326 327createAsset(displayName: string, options: PhotoCreateOptions): Promise<PhotoAsset> 328 329Creates an image or video asset with the specified file name and options. This API uses a promise to return the result. 330 331**System API**: This is a system API. 332 333**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 334 335**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 336 337**Parameters** 338 339| Name | Type | Mandatory| Description | 340| -------- | ------------------------ | ---- | ------------------------- | 341| displayName | string | Yes | File name of the image or video to create. | 342| options | [PhotoCreateOptions](#photocreateoptions) | Yes | Options for creating an image or video asset. | 343 344**Return value** 345 346| Type | Description | 347| --------------------------- | -------------- | 348| Promise<[PhotoAsset](#photoasset)> | Promise used to return the created image and video asset.| 349 350**Error codes** 351 352For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 353 354| ID| Error Message| 355| -------- | ---------------------------------------- | 356| 202 | Called by non-system application. | 357| 401 | if parameter is invalid. | 358| 13900012 | Permission denied. | 359| 13900020 | Invalid argument. | 360| 14000001 | Invalid display name. | 361| 14000011 | System inner fail. | 362 363**Example** 364 365```ts 366async function example() { 367 console.info('createAssetDemo'); 368 try { 369 let testFileName:string = 'testFile' + Date.now() + '.jpg'; 370 let createOption: photoAccessHelper.PhotoCreateOptions = { 371 subtype: photoAccessHelper.PhotoSubtype.DEFAULT 372 } 373 let photoAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(testFileName, createOption); 374 console.info('createAsset file displayName' + photoAsset.displayName); 375 console.info('createAsset successfully'); 376 } catch (err) { 377 console.error('createAsset failed, message = ', err); 378 } 379} 380``` 381 382### createAsset 383 384createAsset(photoType: PhotoType, extension: string, options: CreateOptions, callback: AsyncCallback<string>): void 385 386Creates an image or video asset with the specified file type, file name extension, and options. This API uses an asynchronous callback to return the result. 387 388If the application does not have the **ohos.permission.WRITE_IMAGEVIDEO** permission, you can use a security component to create a media asset. For details, see [Creating a Media Asset Using a Security Component](../../file-management/photoAccessHelper-resource-guidelines.md#creating-a-media-asset-using-a-security-component). 389 390**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 391 392**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 393 394**Parameters** 395 396| Name | Type | Mandatory| Description | 397| -------- | ------------------------ | ---- | ------------------------- | 398| photoType | [PhotoType](#phototype) | Yes | Type of the file to create, which can be **IMAGE** or **VIDEO**. | 399| extension | string | Yes | File name extension, for example, **jpg**. | 400| options | [CreateOptions](#createoptions) | Yes | Options for creating the image or video asset, for example, **{title: 'testPhoto'}**. | 401| callback | AsyncCallback<string> | Yes | Callback invoked to return the URI of the created image or video.| 402 403**Error codes** 404 405For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 406 407| ID| Error Message| 408| -------- | ---------------------------------------- | 409| 401 | if parameter is invalid. | 410| 13900012 | Permission denied. | 411| 13900020 | Invalid argument. | 412| 14000011 | System inner fail. | 413 414**Example** 415 416```ts 417async function example() { 418 console.info('createAssetDemo'); 419 let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; 420 let extension:string = 'jpg'; 421 let options: photoAccessHelper.CreateOptions = { 422 title: 'testPhoto' 423 } 424 phAccessHelper.createAsset(photoType, extension, options, (err, uri) => { 425 if (uri !== undefined) { 426 console.info('createAsset uri' + uri); 427 console.info('createAsset successfully'); 428 } else { 429 console.error('createAsset failed, message = ', err); 430 } 431 }); 432} 433``` 434 435### createAsset 436 437createAsset(photoType: PhotoType, extension: string, callback: AsyncCallback<string>): void 438 439Creates an image or video asset with the specified file type and file name extension. This API uses an asynchronous callback to return the result. 440 441If the application does not have the **ohos.permission.WRITE_IMAGEVIDEO** permission, you can use a security component to create a media asset. For details, see [Creating a Media Asset Using a Security Component](../../file-management/photoAccessHelper-resource-guidelines.md#creating-a-media-asset-using-a-security-component). 442 443**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 444 445**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 446 447**Parameters** 448 449| Name | Type | Mandatory| Description | 450| -------- | ------------------------ | ---- | ------------------------- | 451| photoType | [PhotoType](#phototype) | Yes | Type of the file to create, which can be **IMAGE** or **VIDEO**. | 452| extension | string | Yes | File name extension, for example, **jpg**. | 453| callback | AsyncCallback<string> | Yes | Callback invoked to return the URI of the created image or video.| 454 455**Error codes** 456 457For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 458 459| ID| Error Message| 460| -------- | ---------------------------------------- | 461| 401 | if parameter is invalid. | 462| 13900012 | Permission denied. | 463| 13900020 | Invalid argument. | 464| 14000011 | System inner fail. | 465 466**Example** 467 468```ts 469async function example() { 470 console.info('createAssetDemo'); 471 let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; 472 let extension: string = 'jpg'; 473 phAccessHelper.createAsset(photoType, extension, (err, uri) => { 474 if (uri !== undefined) { 475 console.info('createAsset uri' + uri); 476 console.info('createAsset successfully'); 477 } else { 478 console.error('createAsset failed, message = ', err); 479 } 480 }); 481} 482``` 483 484### createAsset 485 486createAsset(photoType: PhotoType, extension: string, options?: CreateOptions): Promise<string> 487 488Creates an image or video asset with the specified file type, file name extension, and options. This API uses a promise to return the result. 489 490If the application does not have the **ohos.permission.WRITE_IMAGEVIDEO** permission, you can use a security component to create a media asset. For details, see [Creating a Media Asset Using a Security Component](../../file-management/photoAccessHelper-resource-guidelines.md#creating-a-media-asset-using-a-security-component). 491 492**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 493 494**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 495 496**Parameters** 497 498| Name | Type | Mandatory| Description | 499| -------- | ------------------------ | ---- | ------------------------- | 500| photoType | [PhotoType](#phototype) | Yes | Type of the file to create, which can be **IMAGE** or **VIDEO**. | 501| extension | string | Yes | File name extension, for example, **jpg**. | 502| options | [CreateOptions](#createoptions) | No | Options for creating the image or video asset, for example, **{title: 'testPhoto'}**. | 503 504**Return value** 505 506| Type | Description | 507| --------------------------- | -------------- | 508| Promise<string> | Promise used to return the URI of the created image or video asset.| 509 510**Error codes** 511 512For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 513 514| ID| Error Message| 515| -------- | ---------------------------------------- | 516| 401 | if parameter is invalid. | 517| 13900012 | Permission denied. | 518| 13900020 | Invalid argument. | 519| 14000011 | System inner fail. | 520 521**Example** 522 523```ts 524async function example() { 525 console.info('createAssetDemo'); 526 try { 527 let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; 528 let extension: string = 'jpg'; 529 let options: photoAccessHelper.CreateOptions = { 530 title: 'testPhoto' 531 } 532 let uri: string = await phAccessHelper.createAsset(photoType, extension, options); 533 console.info('createAsset uri' + uri); 534 console.info('createAsset successfully'); 535 } catch (err) { 536 console.error('createAsset failed, message = ', err); 537 } 538} 539``` 540 541### createAlbum 542 543createAlbum(name: string, callback: AsyncCallback<Album>): void 544 545Creates an album. This API uses an asynchronous callback to return the result. 546 547The album name must meet the following requirements: 548- The album name cannot exceed 255 characters. 549- The album name cannot contain any of the following characters:<br>. .. \ / : * ? " ' ` < > | { } [ ] 550- The album name is case-insensitive. 551- Duplicate album names are not allowed. 552 553**System API**: This is a system API. 554 555**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 556 557**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 558 559**Parameters** 560 561| Name | Type | Mandatory| Description | 562| -------- | ------------------------ | ---- | ------------------------- | 563| name | string | Yes | Name of the album to create. | 564| callback | AsyncCallback<[Album](#album)> | Yes | Callback invoked to return the created album instance.| 565 566**Error codes** 567 568For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 569 570| ID| Error Message| 571| -------- | ---------------------------------------- | 572| 202 | Called by non-system application. | 573| 401 | if parameter is invalid. | 574| 13900012 | Permission denied. | 575| 13900015 | File exists. | 576| 13900020 | Invalid argument. | 577| 14000011 | System inner fail. | 578 579**Example** 580 581```ts 582async function example() { 583 console.info('createAlbumDemo'); 584 let albumName: string = 'newAlbumName' + new Date().getTime(); 585 phAccessHelper.createAlbum(albumName, (err, album) => { 586 if (err) { 587 console.error('createAlbumCallback failed with err: ' + err); 588 return; 589 } 590 console.info('createAlbumCallback successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri); 591 }); 592} 593``` 594 595### createAlbum 596 597createAlbum(name: string): Promise<Album> 598 599Creates an album. This API uses a promise to return the result. 600 601The album name must meet the following requirements: 602- The album name cannot exceed 255 characters. 603- The album name cannot contain any of the following characters:<br>. .. \ / : * ? " ' ` < > | { } [ ] 604- The album name is case-insensitive. 605- Duplicate album names are not allowed. 606 607**System API**: This is a system API. 608 609**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 610 611**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 612 613**Parameters** 614 615| Name | Type | Mandatory| Description | 616| -------- | ------------------------ | ---- | ------------------------- | 617| name | string | Yes | Name of the album to create. | 618 619**Return value** 620 621| Type | Description | 622| --------------------------- | -------------- | 623| Promise<[Album](#album)> | Promise used to return the created album instance.| 624 625**Error codes** 626 627For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 628 629| ID| Error Message| 630| -------- | ---------------------------------------- | 631| 202 | Called by non-system application. | 632| 401 | if parameter is invalid. | 633| 13900012 | Permission denied. | 634| 13900015 | File exists. | 635| 13900020 | Invalid argument. | 636| 14000011 | System inner fail. | 637 638**Example** 639 640```ts 641import { BusinessError } from '@ohos.base'; 642 643async function example() { 644 console.info('createAlbumDemo'); 645 let albumName: string = 'newAlbumName' + new Date().getTime(); 646 phAccessHelper.createAlbum(albumName).then((album) => { 647 console.info('createAlbumPromise successfully, album: ' + album.albumName + ' album uri: ' + album.albumUri); 648 }).catch((err: BusinessError) => { 649 console.error('createAlbumPromise failed with err: ' + err); 650 }); 651} 652``` 653 654### deleteAlbums 655 656deleteAlbums(albums: Array<Album>, callback: AsyncCallback<void>): void 657 658Deletes albums. This API uses an asynchronous callback to return the result. 659 660Ensure that the albums to be deleted exist. Only user albums can be deleted. 661 662**System API**: This is a system API. 663 664**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 665 666**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 667 668**Parameters** 669 670| Name | Type | Mandatory| Description | 671| -------- | ------------------------ | ---- | ------------------------- | 672| albums | Array<[Album](#album)> | Yes | Albums to delete. | 673| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 674 675**Error codes** 676 677For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 678 679| ID| Error Message| 680| -------- | ---------------------------------------- | 681| 202 | Called by non-system application. | 682| 401 | if parameter is invalid. | 683| 13900012 | Permission denied. | 684| 13900020 | Invalid argument. | 685| 14000011 | System inner fail. | 686 687**Example** 688 689```ts 690import dataSharePredicates from '@ohos.data.dataSharePredicates'; 691 692async function example() { 693 // Delete the album named newAlbumName. 694 console.info('deleteAlbumsDemo'); 695 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 696 predicates.equalTo('album_name', 'newAlbumName'); 697 let fetchOptions: photoAccessHelper.FetchOptions = { 698 fetchColumns: [], 699 predicates: predicates 700 }; 701 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); 702 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 703 phAccessHelper.deleteAlbums([album], (err) => { 704 if (err) { 705 console.error('deletePhotoAlbumsCallback failed with err: ' + err); 706 return; 707 } 708 console.info('deletePhotoAlbumsCallback successfully'); 709 }); 710 fetchResult.close(); 711} 712``` 713 714### deleteAlbums 715 716deleteAlbums(albums: Array<Album>): Promise<void> 717 718Deletes albums. This API uses a promise to return the result. 719 720Ensure that the albums to be deleted exist. Only user albums can be deleted. 721 722**System API**: This is a system API. 723 724**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 725 726**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 727 728**Parameters** 729 730| Name | Type | Mandatory| Description | 731| -------- | ------------------------ | ---- | ------------------------- | 732| albums | Array<[Album](#album)> | Yes | Albums to delete. | 733 734**Return value** 735 736| Type | Description | 737| --------------------------- | -------------- | 738| Promise<void> | Promise that returns no value.| 739 740**Error codes** 741 742For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 743 744| ID| Error Message| 745| -------- | ---------------------------------------- | 746| 202 | Called by non-system application. | 747| 401 | if parameter is invalid. | 748| 13900012 | Permission denied. | 749| 13900020 | Invalid argument. | 750| 14000011 | System inner fail. | 751 752**Example** 753 754```ts 755import dataSharePredicates from '@ohos.data.dataSharePredicates'; 756import { BusinessError } from '@ohos.base'; 757 758async function example() { 759 // Delete the album named newAlbumName. 760 console.info('deleteAlbumsDemo'); 761 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 762 predicates.equalTo('album_name', 'newAlbumName'); 763 let fetchOptions: photoAccessHelper.FetchOptions = { 764 fetchColumns: [], 765 predicates: predicates 766 }; 767 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions); 768 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 769 phAccessHelper.deleteAlbums([album]).then(() => { 770 console.info('deletePhotoAlbumsPromise successfully'); 771 }).catch((err: BusinessError) => { 772 console.error('deletePhotoAlbumsPromise failed with err: ' + err); 773 }); 774 fetchResult.close(); 775} 776``` 777 778### getAlbums 779 780getAlbums(type: AlbumType, subtype: AlbumSubtype, options: FetchOptions, callback: AsyncCallback<FetchResult<Album>>): void 781 782Obtains albums based on the specified options and album type. This API uses an asynchronous callback to return the result. 783 784Before the operation, ensure that the albums to obtain exist. 785 786**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 787 788**Required permissions**: ohos.permission.READ_IMAGEVIDEO 789 790**Parameters** 791 792| Name | Type | Mandatory| Description | 793| -------- | ------------------------ | ---- | ------------------------- | 794| type | [AlbumType](#albumtype) | Yes | Type of the album to obtain. | 795| subtype | [AlbumSubtype](#albumsubtype) | Yes | Subtype of the album. | 796| options | [FetchOptions](#fetchoptions) | Yes | Options for fetching the albums. | 797| callback | AsyncCallback<[FetchResult](#fetchresult)<[Album](#album)>> | Yes | Callback invoked to return the result.| 798 799**Error codes** 800 801For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 802 803| ID| Error Message| 804| -------- | ---------------------------------------- | 805| 401 | if parameter is invalid. | 806| 13900012 | Permission denied. | 807| 13900020 | Invalid argument. | 808| 14000011 | System inner fail. | 809 810**Example** 811 812```ts 813import dataSharePredicates from '@ohos.data.dataSharePredicates'; 814 815async function example() { 816 // Obtain the album named newAlbumName. 817 console.info('getAlbumsDemo'); 818 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 819 predicates.equalTo('album_name', 'newAlbumName'); 820 let fetchOptions: photoAccessHelper.FetchOptions = { 821 fetchColumns: [], 822 predicates: predicates 823 }; 824 phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions, async (err, fetchResult) => { 825 if (err) { 826 console.error('getAlbumsCallback failed with err: ' + err); 827 return; 828 } 829 if (fetchResult === undefined) { 830 console.error('getAlbumsCallback fetchResult is undefined'); 831 return; 832 } 833 let album = await fetchResult.getFirstObject(); 834 console.info('getAlbumsCallback successfully, albumName: ' + album.albumName); 835 fetchResult.close(); 836 }); 837} 838``` 839 840### getAlbums 841 842getAlbums(type: AlbumType, subtype: AlbumSubtype, callback: AsyncCallback<FetchResult<Album>>): void 843 844Obtains albums by type. This API uses an asynchronous callback to return the result. 845 846Before the operation, ensure that the albums to obtain exist. 847 848**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 849 850**Required permissions**: ohos.permission.READ_IMAGEVIDEO 851 852**Parameters** 853 854| Name | Type | Mandatory| Description | 855| -------- | ------------------------ | ---- | ------------------------- | 856| type | [AlbumType](#albumtype) | Yes | Type of the album to obtain. | 857| subtype | [AlbumSubtype](#albumsubtype) | Yes | Subtype of the album. | 858| callback | AsyncCallback<[FetchResult](#fetchresult)<[Album](#album)>> | Yes | Callback invoked to return the result.| 859 860**Error codes** 861 862For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 863 864| ID| Error Message| 865| -------- | ---------------------------------------- | 866| 401 | if parameter is invalid. | 867| 13900012 | Permission denied. | 868| 13900020 | Invalid argument. | 869| 14000011 | System inner fail. | 870 871**Example** 872 873```ts 874async function example() { 875 // Obtain the system album VIDEO, which is preset by default. 876 console.info('getAlbumsDemo'); 877 phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO, async (err, fetchResult) => { 878 if (err) { 879 console.error('getAlbumsCallback failed with err: ' + err); 880 return; 881 } 882 if (fetchResult === undefined) { 883 console.error('getAlbumsCallback fetchResult is undefined'); 884 return; 885 } 886 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 887 console.info('getAlbumsCallback successfully, albumUri: ' + album.albumUri); 888 fetchResult.close(); 889 }); 890} 891``` 892 893### getAlbums 894 895getAlbums(type: AlbumType, subtype: AlbumSubtype, options?: FetchOptions): Promise<FetchResult<Album>> 896 897Obtains albums based on the specified options and album type. This API uses a promise to return the result. 898 899Before the operation, ensure that the albums to obtain exist. 900 901**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 902 903**Required permissions**: ohos.permission.READ_IMAGEVIDEO 904 905**Parameters** 906 907| Name | Type | Mandatory| Description | 908| -------- | ------------------------ | ---- | ------------------------- | 909| type | [AlbumType](#albumtype) | Yes | Type of the album to obtain. | 910| subtype | [AlbumSubtype](#albumsubtype) | Yes | Subtype of the album. | 911| options | [FetchOptions](#fetchoptions) | No | Options for fetching the albums. If this parameter is not specified, the albums are obtained based on the album type by default. | 912 913**Return value** 914 915| Type | Description | 916| --------------------------- | -------------- | 917| Promise<[FetchResult](#fetchresult)<[Album](#album)>> | Promise used to return the result.| 918 919**Error codes** 920 921For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 922 923| ID| Error Message| 924| -------- | ---------------------------------------- | 925| 401 | if parameter is invalid. | 926| 13900012 | Permission denied. | 927| 13900020 | Invalid argument. | 928| 14000011 | System inner fail. | 929 930**Example** 931 932```ts 933import dataSharePredicates from '@ohos.data.dataSharePredicates'; 934import { BusinessError } from '@ohos.base'; 935 936async function example() { 937 // Obtain the album named newAlbumName. 938 console.info('getAlbumsDemo'); 939 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 940 predicates.equalTo('album_name', 'newAlbumName'); 941 let fetchOptions: photoAccessHelper.FetchOptions = { 942 fetchColumns: [], 943 predicates: predicates 944 }; 945 phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions).then( async (fetchResult) => { 946 if (fetchResult === undefined) { 947 console.error('getAlbumsPromise fetchResult is undefined'); 948 return; 949 } 950 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 951 console.info('getAlbumsPromise successfully, albumName: ' + album.albumName); 952 fetchResult.close(); 953 }).catch((err: BusinessError) => { 954 console.error('getAlbumsPromise failed with err: ' + err); 955 }); 956} 957``` 958 959### deleteAssets 960 961deleteAssets(uriList: Array<string>, callback: AsyncCallback<void>): void 962 963Deletes media assets. This API uses an asynchronous callback to return the result. The deleted assets are moved to the trash. 964 965**System API**: This is a system API. 966 967**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 968 969**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 970 971**Parameters** 972 973| Name | Type | Mandatory| Description | 974| -------- | ------------------------- | ---- | ---------- | 975| uriList | Array<string> | Yes | URIs of the media files to delete.| 976| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 977 978**Error codes** 979 980For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 981 982| ID| Error Message| 983| -------- | ---------------------------------------- | 984| 202 | Called by non-system application. | 985| 401 | if parameter is invalid. | 986| 13900012 | Permission denied. | 987| 13900020 | Invalid argument. | 988| 14000002 | Invalid uri. | 989| 14000011 | System inner fail. | 990 991**Example** 992 993```ts 994import dataSharePredicates from '@ohos.data.dataSharePredicates'; 995 996async function example() { 997 console.info('deleteAssetDemo'); 998 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 999 let fetchOptions: photoAccessHelper.FetchOptions = { 1000 fetchColumns: [], 1001 predicates: predicates 1002 }; 1003 try { 1004 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1005 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1006 if (asset === undefined) { 1007 console.error('asset not exist'); 1008 return; 1009 } 1010 phAccessHelper.deleteAssets([asset.uri], (err) => { 1011 if (err === undefined) { 1012 console.info('deleteAssets successfully'); 1013 } else { 1014 console.error('deleteAssets failed with error: ' + err); 1015 } 1016 }); 1017 } catch (err) { 1018 console.error('fetch failed, message =', err); 1019 } 1020} 1021``` 1022 1023### deleteAssets 1024 1025deleteAssets(uriList: Array<string>): Promise<void> 1026 1027Deletes media assets. This API uses a promise to return the result. The deleted assets are moved to the trash. 1028 1029**System API**: This is a system API. 1030 1031**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 1032 1033**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1034 1035**Parameters** 1036 1037| Name | Type | Mandatory| Description | 1038| -------- | ------------------------- | ---- | ---------- | 1039| uriList | Array<string> | Yes | URIs of the media files to delete.| 1040 1041**Return value** 1042 1043| Type | Description | 1044| --------------------------------------- | ----------------- | 1045| Promise<void>| Promise that returns no value.| 1046 1047**Error codes** 1048 1049For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1050 1051| ID| Error Message| 1052| -------- | ---------------------------------------- | 1053| 202 | Called by non-system application. | 1054| 401 | if parameter is invalid. | 1055| 13900012 | Permission denied. | 1056| 13900020 | Invalid argument. | 1057| 14000002 | Invalid uri. | 1058| 14000011 | System inner fail. | 1059 1060**Example** 1061 1062```ts 1063import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1064 1065async function example() { 1066 console.info('deleteDemo'); 1067 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1068 let fetchOptions: photoAccessHelper.FetchOptions = { 1069 fetchColumns: [], 1070 predicates: predicates 1071 }; 1072 try { 1073 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1074 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1075 if (asset === undefined) { 1076 console.error('asset not exist'); 1077 return; 1078 } 1079 await phAccessHelper.deleteAssets([asset.uri]); 1080 console.info('deleteAssets successfully'); 1081 } catch (err) { 1082 console.error('deleteAssets failed with error: ' + err); 1083 } 1084} 1085``` 1086 1087### registerChange 1088 1089registerChange(uri: string, forChildUris: boolean, callback: Callback<ChangeData>) : void 1090 1091Registers listening for the specified URI. This API uses a callback to return the result. 1092 1093**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1094 1095**Parameters** 1096 1097| Name | Type | Mandatory| Description | 1098| --------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 1099| uri | string | Yes | URI of the photo asset, URI of the album, or [DefaultChangeUri](#defaultchangeuri).| 1100| forChildUris | boolean | Yes | Whether to perform fuzzy listening.<br>If **uri** is the URI of an album, the value **true** means to listen for the changes of the files in the album; the value **false** means to listen for the changes of the album only. <br>If **uri** is the URI of a **photoAsset**, there is no difference between **true** and **false** for **forChildUris**.<br>If **uri** is **DefaultChangeUri**, **forChildUris** must be set to **true**. If **forChildUris** is **false**, the URI cannot be found and no message can be received.| 1101| callback | Callback<[ChangeData](#changedata)> | Yes | Callback invoked to return the [ChangeData](#changedata). <br>**NOTE**<br>Multiple callback listeners can be registered for a URI. You can use [unRegisterChange](#unregisterchange) to unregister all listeners for the URI or a specified callback listener.| 1102 1103**Error codes** 1104 1105For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1106 1107| ID| Error Message| 1108| -------- | ---------------------------------------- | 1109| 401 | if parameter is invalid. | 1110| 13900012 | Permission denied. | 1111| 13900020 | Invalid argument. | 1112 1113**Example** 1114 1115```ts 1116import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1117 1118async function example() { 1119 console.info('registerChangeDemo'); 1120 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1121 let fetchOptions: photoAccessHelper.FetchOptions = { 1122 fetchColumns: [], 1123 predicates: predicates 1124 }; 1125 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1126 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1127 if (photoAsset !== undefined) { 1128 console.info('photoAsset.displayName : ' + photoAsset.displayName); 1129 } 1130 let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { 1131 console.info('onCallback1 success, changData: ' + JSON.stringify(changeData)); 1132 //file had changed, do something 1133 } 1134 let onCallback2 = (changeData: photoAccessHelper.ChangeData) => { 1135 console.info('onCallback2 success, changData: ' + JSON.stringify(changeData)); 1136 //file had changed, do something 1137 } 1138 // Register onCallback1. 1139 phAccessHelper.registerChange(photoAsset.uri, false, onCallback1); 1140 // Register onCallback2. 1141 phAccessHelper.registerChange(photoAsset.uri, false, onCallback2); 1142 1143 photoAsset.setFavorite(true, (err) => { 1144 if (err === undefined) { 1145 console.info('setFavorite successfully'); 1146 } else { 1147 console.error('setFavorite failed with error:' + err); 1148 } 1149 }); 1150} 1151``` 1152 1153### unRegisterChange 1154 1155unRegisterChange(uri: string, callback?: Callback<ChangeData>): void 1156 1157Unregisters listening for the specified URI. Multiple callbacks can be registered for a URI for listening. You can use this API to unregister the listening of the specified callbacks or all callbacks. 1158 1159**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1160 1161**Parameters** 1162 1163| Name | Type | Mandatory| Description | 1164| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 1165| uri | string | Yes | URI of the photo asset, URI of the album, or [DefaultChangeUri](#defaultchangeuri).| 1166| callback | Callback<[ChangeData](#changedata)> | No | Callback to unregister. If this parameter is not specified, all the callbacks for listening for the URI will be canceled. <br>**NOTE**: The specified callback unregistered will not be invoked when the data changes.| 1167 1168**Error codes** 1169 1170For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1171 1172| ID| Error Message| 1173| -------- | ---------------------------------------- | 1174| 401 | if parameter is invalid. | 1175| 13900012 | Permission denied. | 1176| 13900020 | Invalid argument. | 1177 1178**Example** 1179 1180```ts 1181import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1182 1183async function example() { 1184 console.info('offDemo'); 1185 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1186 let fetchOptions: photoAccessHelper.FetchOptions = { 1187 fetchColumns: [], 1188 predicates: predicates 1189 }; 1190 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1191 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1192 if (photoAsset !== undefined) { 1193 console.info('photoAsset.displayName : ' + photoAsset.displayName); 1194 } 1195 let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { 1196 console.info('onCallback1 on'); 1197 } 1198 let onCallback2 = (changeData: photoAccessHelper.ChangeData) => { 1199 console.info('onCallback2 on'); 1200 } 1201 // Register onCallback1. 1202 phAccessHelper.registerChange(photoAsset.uri, false, onCallback1); 1203 // Register onCallback2. 1204 phAccessHelper.registerChange(photoAsset.uri, false, onCallback2); 1205 // Unregister the listening of onCallback1. 1206 phAccessHelper.unRegisterChange(photoAsset.uri, onCallback1); 1207 photoAsset.setFavorite(true, (err) => { 1208 if (err === undefined) { 1209 console.info('setFavorite successfully'); 1210 } else { 1211 console.error('setFavorite failed with error:' + err); 1212 } 1213 }); 1214} 1215``` 1216 1217### createDeleteRequest 1218 1219createDeleteRequest(uriList: Array<string>, callback: AsyncCallback<void>): void 1220 1221Creates a dialog box for deleting media files. This API uses an asynchronous callback to return the result. The deleted media files are moved to the trash. 1222 1223**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 1224 1225**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1226 1227**Parameters** 1228 1229| Name | Type | Mandatory| Description | 1230| -------- | ------------------------- | ---- | ---------- | 1231| uriList | Array<string> | Yes | URIs of the media files to delete. A maximum of 300 media files can be deleted.| 1232| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 1233 1234**Error codes** 1235 1236For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1237 1238| ID| Error Message| 1239| -------- | ---------------------------------------- | 1240| 401 | if parameter is invalid. | 1241| 13900012 | Permission denied. | 1242| 13900020 | Invalid argument. | 1243| 14000011 | System inner fail. | 1244 1245**Example** 1246 1247```ts 1248import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1249 1250async function example() { 1251 console.info('createDeleteRequestDemo'); 1252 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1253 let fetchOptions: photoAccessHelper.FetchOptions = { 1254 fetchColumns: [], 1255 predicates: predicates 1256 }; 1257 try { 1258 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1259 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1260 if (asset === undefined) { 1261 console.error('asset not exist'); 1262 return; 1263 } 1264 phAccessHelper.createDeleteRequest([asset.uri], (err) => { 1265 if (err === undefined) { 1266 console.info('createDeleteRequest successfully'); 1267 } else { 1268 console.error('createDeleteRequest failed with error: ' + err); 1269 } 1270 }); 1271 } catch (err) { 1272 console.info('fetch failed, message =', err); 1273 } 1274} 1275``` 1276 1277### createDeleteRequest 1278 1279createDeleteRequest(uriList: Array<string>): Promise<void> 1280 1281Creates a dialog box for deleting media files. This API uses a promise to return the result. The deleted media files are moved to the trash. 1282 1283**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 1284 1285**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1286 1287**Parameters** 1288 1289| Name | Type | Mandatory| Description | 1290| -------- | ------------------------- | ---- | ---------- | 1291| uriList | Array<string> | Yes | URIs of the media files to delete. A maximum of 300 media files can be deleted.| 1292 1293**Return value** 1294 1295| Type | Description | 1296| --------------------------------------- | ----------------- | 1297| Promise<void>| Promise that returns no value.| 1298 1299**Error codes** 1300 1301For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1302 1303| ID| Error Message| 1304| -------- | ---------------------------------------- | 1305| 401 | if parameter is invalid. | 1306| 13900012 | Permission denied. | 1307| 13900020 | Invalid argument. | 1308| 14000011 | System inner fail. | 1309 1310**Example** 1311 1312```ts 1313import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1314 1315async function example() { 1316 console.info('createDeleteRequestDemo'); 1317 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1318 let fetchOptions: photoAccessHelper.FetchOptions = { 1319 fetchColumns: [], 1320 predicates: predicates 1321 }; 1322 try { 1323 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1324 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1325 if (asset === undefined) { 1326 console.error('asset not exist'); 1327 return; 1328 } 1329 await phAccessHelper.createDeleteRequest([asset.uri]); 1330 console.info('createDeleteRequest successfully'); 1331 } catch (err) { 1332 console.error('createDeleteRequest failed with error: ' + err); 1333 } 1334} 1335``` 1336 1337### getPhotoIndex 1338 1339getPhotoIndex(photoUri: string, albumUri: string, options: FetchOptions, callback: AsyncCallback<number>): void 1340 1341Obtains the index of an image or video in an album. This API uses an asynchronous callback to return the result. 1342 1343**System API**: This is a system API. 1344 1345**Required permissions**: ohos.permission.READ_IMAGEVIDEO 1346 1347**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1348 1349**Parameters** 1350 1351| Name | Type | Mandatory| Description | 1352| -------- | ------------------------- | ---- | ---------- | 1353| photoUri | string | Yes | URI of the media asset whose index is to be obtained.| 1354| albumUri | string | Yes | Album URI, which can be an empty string. If it is an empty string, all the media assets in the Gallery are obtained by default. | 1355| options | [FetchOptions](#fetchoptions) | Yes | Fetch options. Only one search condition or sorting mode must be set in **predicates**. If no value is set or multiple search criteria or sorting modes are set, the API cannot be called successfully. | 1356| callback | AsyncCallback<number>| Yes | Callback invoked to return the index obtained.| 1357 1358**Error codes** 1359 1360For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1361 1362| ID| Error Message| 1363| -------- | ---------------------------------------- | 1364| 202 | Called by non-system application. | 1365| 401 | if parameter is invalid. | 1366| 13900012 | Permission denied. | 1367| 13900020 | Invalid argument. | 1368| 14000011 | System inner fail. | 1369 1370**Example** 1371 1372```ts 1373import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1374 1375async function example() { 1376 try { 1377 console.info('getPhotoIndexDemo'); 1378 let predicatesForGetAsset: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1379 let fetchOp: photoAccessHelper.FetchOptions = { 1380 fetchColumns: [], 1381 predicates: predicatesForGetAsset 1382 }; 1383 // Obtain the uri of the album 1384 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE, fetchOp); 1385 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 1386 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1387 predicates.orderByAsc(photoAccessHelper.PhotoKeys.DATE_MODIFIED); 1388 let fetchOptions: photoAccessHelper.FetchOptions = { 1389 fetchColumns: [photoAccessHelper.PhotoKeys.DATE_MODIFIED], 1390 predicates: predicates 1391 }; 1392 let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions); 1393 let expectIndex = 1; 1394 // Obtain the uri of the second file 1395 let photoAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getObjectByPosition(expectIndex); 1396 1397 phAccessHelper.getPhotoIndex(photoAsset.uri, album.albumUri, fetchOptions, (err, index) => { 1398 if (err === undefined) { 1399 console.info(`getPhotoIndex successfully and index is : ${index}`); 1400 } else { 1401 console.info(`getPhotoIndex failed;`); 1402 } 1403 }); 1404 } catch (error) { 1405 console.info(`getPhotoIndex failed; error: ${error}`); 1406 } 1407} 1408``` 1409 1410### getPhotoIndex 1411 1412getPhotoIndex(photoUri: string, albumUri: string, options: FetchOptions): Promise<number> 1413 1414Obtains the index of an image or video in an album. This API uses a promise to return the result. 1415 1416**System API**: This is a system API. 1417 1418**Required permissions**: ohos.permission.READ_IMAGEVIDEO 1419 1420**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1421 1422**Parameters** 1423 1424| Name | Type | Mandatory| Description | 1425| -------- | ------------------------- | ---- | ---------- | 1426| photoUri | string | Yes | URI of the media asset whose index is to be obtained.| 1427| albumUri | string | Yes | Album URI, which can be an empty string. If it is an empty string, all the media assets in the Gallery are obtained by default. | 1428| options | [FetchOptions](#fetchoptions) | Yes | Fetch options. Only one search condition or sorting mode must be set in **predicates**. If no value is set or multiple search criteria or sorting modes are set, the API cannot be called successfully. | 1429 1430**Return value** 1431 1432| Type | Description | 1433| --------------------------------------- | ----------------- | 1434| Promise<number>| Promise used to return the index obtained.| 1435 1436**Error codes** 1437 1438For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1439 1440| ID| Error Message| 1441| -------- | ---------------------------------------- | 1442| 202 | Called by non-system application. | 1443| 401 | if parameter is invalid. | 1444| 13900012 | Permission denied. | 1445| 13900020 | Invalid argument. | 1446| 14000011 | System inner fail. | 1447 1448**Example** 1449 1450```ts 1451import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1452import { BusinessError } from '@ohos.base'; 1453 1454async function example() { 1455 try { 1456 console.info('getPhotoIndexDemo'); 1457 let predicatesForGetAsset: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1458 let fetchOp: photoAccessHelper.FetchOptions = { 1459 fetchColumns: [], 1460 predicates: predicatesForGetAsset 1461 }; 1462 // Obtain the uri of the album 1463 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.FAVORITE, fetchOp); 1464 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 1465 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1466 predicates.orderByAsc(photoAccessHelper.PhotoKeys.DATE_MODIFIED); 1467 let fetchOptions: photoAccessHelper.FetchOptions = { 1468 fetchColumns: [photoAccessHelper.PhotoKeys.DATE_MODIFIED], 1469 predicates: predicates 1470 }; 1471 let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOptions); 1472 let expectIndex = 1; 1473 // Obtain the uri of the second file 1474 let photoAsset: photoAccessHelper.PhotoAsset = await photoFetchResult.getObjectByPosition(expectIndex); 1475 phAccessHelper.getPhotoIndex(photoAsset.uri, album.albumUri, fetchOptions).then((index) => { 1476 console.info(`getPhotoIndex successfully and index is : ${index}`); 1477 }).catch((err: BusinessError) => { 1478 console.info(`getPhotoIndex failed; error: ${err}`); 1479 }); 1480 } catch (error) { 1481 console.info(`getPhotoIndex failed; error: ${error}`); 1482 } 1483} 1484``` 1485 1486### release 1487 1488release(callback: AsyncCallback<void>): void 1489 1490Releases this **PhotoAccessHelper** instance. This API uses an asynchronous callback to return the result. 1491Call this API when the APIs of the **PhotoAccessHelper** instance are no longer used. 1492 1493**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1494 1495**Parameters** 1496 1497| Name | Type | Mandatory| Description | 1498| -------- | ------------------------- | ---- | -------------------- | 1499| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 1500 1501**Error codes** 1502 1503For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1504 1505| ID| Error Message| 1506| -------- | ---------------------------------------- | 1507| 401 | if parameter is invalid. | 1508| 13900020 | Invalid argument. | 1509| 14000011 | System inner fail. | 1510 1511**Example** 1512 1513```ts 1514async function example() { 1515 console.info('releaseDemo'); 1516 phAccessHelper.release((err) => { 1517 if (err !== undefined) { 1518 console.error('release failed. message = ', err); 1519 } else { 1520 console.info('release ok.'); 1521 } 1522 }); 1523} 1524``` 1525 1526### release 1527 1528release(): Promise<void> 1529 1530Releases this **PhotoAccessHelper** instance. This API uses a promise to return the result. 1531Call this API when the APIs of the **PhotoAccessHelper** instance are no longer used. 1532 1533**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1534 1535**Return value** 1536 1537| Type | Description | 1538| ------------------- | --------------------------------- | 1539| Promise<void> | Promise that returns no value.| 1540 1541**Error codes** 1542 1543For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1544 1545| ID| Error Message| 1546| -------- | ---------------------------------------- | 1547| 401 | if parameter is invalid. | 1548| 13900020 | Invalid argument. | 1549| 14000011 | System inner fail. | 1550 1551**Example** 1552 1553```ts 1554async function example() { 1555 console.info('releaseDemo'); 1556 try { 1557 await phAccessHelper.release(); 1558 console.info('release ok.'); 1559 } catch (err) { 1560 console.error('release failed. message = ', err); 1561 } 1562} 1563``` 1564 1565## PhotoAsset 1566 1567Provides APIs for encapsulating file asset attributes. 1568 1569### Attributes 1570 1571**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1572 1573| Name | Type | Readable| Writable| Description | 1574| ------------------------- | ------------------------ | ---- | ---- | ------------------------------------------------------ | 1575| uri | string | Yes | No | Media asset URI, for example, **file://media/Photo/1/IMG_datetime_0001/displayName.jpg**. For details, see [Media File URI](../../file-management/user-file-uri-intro.md#media-file-uri). | 1576| photoType | [PhotoType](#phototype) | Yes | No | Type of the file. | 1577| displayName | string | Yes | No | File name, including the file name extension, to display. | 1578 1579### get 1580 1581get(member: string): MemberType; 1582 1583Obtains a **PhotoAsset** member parameter. 1584 1585**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1586 1587**Parameters** 1588 1589| Name | Type | Mandatory | Description | 1590| -------- | ------------------------- | ---- | ----- | 1591| member | string | Yes | Name of the member parameter to obtain. Except **uri**, **photoType**, and **displayName**, you need to pass in [PhotoKeys](#photokeys) in **fetchColumns** in **get()**. For example, to obtain the title attribute, set **fetchColumns: ['title']**.| 1592 1593**Return value** 1594 1595| Type | Description | 1596| ------------------- | --------------------------------- | 1597| [MemberType](#membertype) | **PhotoAsset** member parameter obtained.| 1598 1599**Error codes** 1600 1601For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1602 1603| ID| Error Message| 1604| -------- | ---------------------------------------- | 1605| 401 | if parameter is invalid. | 1606| 13900020 | Invalid argument. | 1607| 14000014 | Member is not a valid PhotoKey. | 1608 1609**Example** 1610 1611```ts 1612import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1613 1614async function example() { 1615 console.info('photoAssetGetDemo'); 1616 try { 1617 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1618 let fetchOption: photoAccessHelper.FetchOptions = { 1619 fetchColumns: ['title'], 1620 predicates: predicates 1621 }; 1622 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 1623 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1624 let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE; 1625 let photoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title.toString()); 1626 console.info('photoAsset Get photoAssetTitle = ', photoAssetTitle); 1627 } catch (err) { 1628 console.error('release failed. message = ', err); 1629 } 1630} 1631``` 1632 1633### set 1634 1635set(member: string, value: string): void 1636 1637Sets a **PhotoAsset** member parameter. 1638 1639**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1640 1641**Parameters** 1642 1643| Name | Type | Mandatory | Description | 1644| -------- | ------------------------- | ---- | ----- | 1645| member | string | Yes | Name of the member parameter to set. For example, **[PhotoKeys](#photokeys).TITLE**.| 1646| value | string | Yes | Member parameter to set. Only the value of **[PhotoKeys](#photokeys).TITLE** can be modified.| 1647 1648**Error codes** 1649 1650For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1651 1652| ID| Error Message| 1653| -------- | ---------------------------------------- | 1654| 401 | if parameter is invalid. | 1655| 13900020 | Invalid argument. | 1656| 14000014 | Member is not a valid PhotoKey. | 1657 1658**Example** 1659 1660```ts 1661import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1662 1663async function example() { 1664 console.info('photoAssetSetDemo'); 1665 try { 1666 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1667 let fetchOption: photoAccessHelper.FetchOptions = { 1668 fetchColumns: ['title'], 1669 predicates: predicates 1670 }; 1671 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 1672 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1673 let title: string = photoAccessHelper.PhotoKeys.TITLE.toString(); 1674 photoAsset.set(title, 'newTitle'); 1675 } catch (err) { 1676 console.error('release failed. message = ', err); 1677 } 1678} 1679``` 1680 1681### commitModify 1682 1683commitModify(callback: AsyncCallback<void>): void 1684 1685Commits the modification on the file metadata to the database. This API uses an asynchronous callback to return the result. 1686 1687**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 1688 1689**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1690 1691**Parameters** 1692 1693| Name | Type | Mandatory | Description | 1694| -------- | ------------------------- | ---- | ----- | 1695| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 1696 1697**Error codes** 1698 1699For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1700 1701| ID| Error Message| 1702| -------- | ---------------------------------------- | 1703| 401 | if values to commit is invalid. | 1704| 13900012 | Permission denied. | 1705| 13900020 | Invalid argument. | 1706| 14000001 | Invalid display name. | 1707| 14000011 | System inner fail. | 1708 1709**Example** 1710 1711```ts 1712import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1713 1714async function example() { 1715 console.info('commitModifyDemo'); 1716 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1717 let fetchOption: photoAccessHelper.FetchOptions = { 1718 fetchColumns: ['title'], 1719 predicates: predicates 1720 }; 1721 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 1722 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1723 let title: string = photoAccessHelper.PhotoKeys.TITLE.toString(); 1724 let photoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); 1725 console.info('photoAsset get photoAssetTitle = ', photoAssetTitle); 1726 photoAsset.set(title, 'newTitle2'); 1727 photoAsset.commitModify((err) => { 1728 if (err === undefined) { 1729 let newPhotoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); 1730 console.info('photoAsset get newPhotoAssetTitle = ', newPhotoAssetTitle); 1731 } else { 1732 console.error('commitModify failed, message =', err); 1733 } 1734 }); 1735} 1736``` 1737 1738### commitModify 1739 1740commitModify(): Promise<void> 1741 1742Commits the modification on the file metadata to the database. This API uses a promise to return the result. 1743 1744**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 1745 1746**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1747 1748**Return value** 1749 1750| Type | Description | 1751| ------------------- | ---------- | 1752| Promise<void> | Promise that returns no value.| 1753 1754**Error codes** 1755 1756For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1757 1758| ID| Error Message| 1759| -------- | ---------------------------------------- | 1760| 401 | if values to commit is invalid. | 1761| 13900012 | Permission denied. | 1762| 13900020 | Invalid argument. | 1763| 14000001 | Invalid display name. | 1764| 14000011 | System inner fail. | 1765 1766**Example** 1767 1768```ts 1769import dataSharePredicates from '@ohos.data.dataSharePredicates'; 1770 1771async function example() { 1772 console.info('commitModifyDemo'); 1773 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1774 let fetchOption: photoAccessHelper.FetchOptions = { 1775 fetchColumns: ['title'], 1776 predicates: predicates 1777 }; 1778 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 1779 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1780 let title: string = photoAccessHelper.PhotoKeys.TITLE.toString(); 1781 let photoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); 1782 console.info('photoAsset get photoAssetTitle = ', photoAssetTitle); 1783 photoAsset.set(title, 'newTitle3'); 1784 try { 1785 await photoAsset.commitModify(); 1786 let newPhotoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); 1787 console.info('photoAsset get newPhotoAssetTitle = ', newPhotoAssetTitle); 1788 } catch (err) { 1789 console.error('release failed. message = ', err); 1790 } 1791} 1792``` 1793 1794### open 1795 1796open(mode: string, callback: AsyncCallback<number>): void 1797 1798Opens this file asset. This API uses an asynchronous callback to return the result. 1799 1800> **NOTE**<br>A file can be opened in only one mode at a time. Use **close()** to close the FD returned when it is not required. 1801 1802**System API**: This is a system API. 1803 1804**Required permissions**: ohos.permission.READ_IMAGEVIDEO or ohos.permission.WRITE_IMAGEVIDEO 1805 1806**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1807 1808**Parameters** 1809 1810| Name | Type | Mandatory | Description | 1811| -------- | --------------------------- | ---- | ----------------------------------- | 1812| mode | string | Yes | Mode for opening the file, which can be **'r'** (read-only), **'w'** (write-only), or **'rw'** (read/write).| 1813| callback | AsyncCallback<number> | Yes | Callback invoked to return the file descriptor (FD) of the file opened. | 1814 1815**Error codes** 1816 1817For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1818 1819| ID| Error Message| 1820| -------- | ---------------------------------------- | 1821| 202 | Called by non-system application. | 1822| 401 | if parameter is invalid. | 1823| 13900012 | Permission denied. | 1824| 13900020 | Invalid argument. | 1825| 14000011 | System inner fail. | 1826 1827**Example** 1828 1829```ts 1830async function example() { 1831 console.info('openDemo'); 1832 let testFileName: string = 'testFile' + Date.now() + '.jpg'; 1833 let photoAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(testFileName); 1834 photoAsset.open('rw', (err, fd) => { 1835 if (fd !== undefined) { 1836 console.info('File fd' + fd); 1837 photoAsset.close(fd); 1838 } else { 1839 console.error('File err' + err); 1840 } 1841 }); 1842} 1843``` 1844 1845### open 1846 1847open(mode: string): Promise<number> 1848 1849Opens this file asset. This API uses a promise to return the result. 1850 1851> **NOTE**<br>A file can be opened in only one mode at a time. Use **close()** to close the FD returned when it is not required. 1852 1853**System API**: This is a system API. 1854 1855**Required permissions**: ohos.permission.READ_IMAGEVIDEO or ohos.permission.WRITE_IMAGEVIDEO 1856 1857**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1858 1859**Parameters** 1860 1861| Name | Type | Mandatory | Description | 1862| ---- | ------ | ---- | ----------------------------------- | 1863| mode | string | Yes | Mode for opening the file, which can be **'r'** (read-only), **'w'** (write-only), or **'rw'** (read/write).| 1864 1865**Return value** 1866 1867| Type | Description | 1868| --------------------- | ------------- | 1869| Promise<number> | Promise used to return the FD of the file opened.| 1870 1871**Error codes** 1872 1873For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1874 1875| ID| Error Message| 1876| -------- | ---------------------------------------- | 1877| 202 | Called by non-system application. | 1878| 401 | if parameter is invalid. | 1879| 13900012 | Permission denied. | 1880| 13900020 | Invalid argument. | 1881| 14000011 | System inner fail. | 1882 1883**Example** 1884 1885```ts 1886async function example() { 1887 console.info('openDemo'); 1888 try { 1889 let testFileName: string = 'testFile' + Date.now() + '.jpg'; 1890 let photoAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(testFileName); 1891 let fd: number = await photoAsset.open('rw'); 1892 if (fd !== undefined) { 1893 console.info('File fd' + fd); 1894 photoAsset.close(fd); 1895 } else { 1896 console.error(' open File fail'); 1897 } 1898 } catch (err) { 1899 console.error('open Demo err' + err); 1900 } 1901} 1902``` 1903 1904### getReadOnlyFd 1905 1906getReadOnlyFd(callback: AsyncCallback<number>): void 1907 1908Opens this file in read-only mode. This API uses an asynchronous callback to return the result. 1909 1910> **NOTE**<br>The returned FD must be closed when it is not required. 1911 1912**Required permissions**: ohos.permission.READ_IMAGEVIDEO 1913 1914**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1915 1916**Parameters** 1917 1918| Name | Type | Mandatory | Description | 1919| -------- | --------------------------- | ---- | ----------------------------------- | 1920| callback | AsyncCallback<number> | Yes | Callback invoked to return the FD of the file opened. | 1921 1922**Error codes** 1923 1924For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1925 1926| ID| Error Message| 1927| -------- | ---------------------------------------- | 1928| 401 | if parameter is invalid. | 1929| 13900012 | Permission denied. | 1930| 13900020 | Invalid argument. | 1931| 14000011 | System inner fail. | 1932 1933**Example** 1934 1935```ts 1936async function example() { 1937 console.info('getReadOnlyFdDemo'); 1938 let testFileName: string = 'testFile' + Date.now() + '.jpg'; 1939 let photoAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(testFileName); 1940 photoAsset.getReadOnlyFd((err, fd) => { 1941 if (fd !== undefined) { 1942 console.info('File fd' + fd); 1943 photoAsset.close(fd); 1944 } else { 1945 console.error('File err' + err); 1946 } 1947 }); 1948} 1949``` 1950 1951### getReadOnlyFd 1952 1953getReadOnlyFd(): Promise<number> 1954 1955Opens this file in read-only mode. This API uses a promise to return the result. 1956 1957> **NOTE**<br>The returned FD must be closed when it is not required. 1958 1959**Required permissions**: ohos.permission.READ_IMAGEVIDEO 1960 1961**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 1962 1963**Return value** 1964 1965| Type | Description | 1966| --------------------- | ------------- | 1967| Promise<number> | Promise used to return the FD of the file opened.| 1968 1969**Error codes** 1970 1971For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 1972 1973| ID| Error Message| 1974| -------- | ---------------------------------------- | 1975| 401 | if parameter is invalid. | 1976| 13900012 | Permission denied. | 1977| 13900020 | Invalid argument. | 1978| 14000011 | System inner fail. | 1979 1980**Example** 1981 1982```ts 1983async function example() { 1984 console.info('getReadOnlyFdDemo'); 1985 try { 1986 let testFileName: string = 'testFile' + Date.now() + '.jpg'; 1987 let photoAsset: photoAccessHelper.PhotoAsset = await phAccessHelper.createAsset(testFileName); 1988 let fd: number = await photoAsset.getReadOnlyFd(); 1989 if (fd !== undefined) { 1990 console.info('File fd' + fd); 1991 photoAsset.close(fd); 1992 } else { 1993 console.error(' open File fail'); 1994 } 1995 } catch (err) { 1996 console.error('open Demo err' + err); 1997 } 1998} 1999``` 2000 2001### close 2002 2003close(fd: number, callback: AsyncCallback<void>): void 2004 2005Closes a file. This API uses an asynchronous callback to return the result. 2006 2007**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2008 2009**Parameters** 2010 2011| Name | Type | Mandatory | Description | 2012| -------- | ------------------------- | ---- | ----- | 2013| fd | number | Yes | FD of the file to close.| 2014| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 2015 2016**Error codes** 2017 2018For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2019 2020| ID| Error Message| 2021| -------- | ---------------------------------------- | 2022| 401 | if parameter is invalid. | 2023| 13900020 | Invalid argument. | 2024| 14000011 | System inner fail. | 2025 2026**Example** 2027 2028```ts 2029import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2030 2031async function example() { 2032 console.info('closeDemo'); 2033 try { 2034 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2035 let fetchOption: photoAccessHelper.FetchOptions = { 2036 fetchColumns: [], 2037 predicates: predicates 2038 }; 2039 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2040 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 2041 let fd: number = await photoAsset.open('rw'); 2042 console.info('file fd', fd); 2043 photoAsset.close(fd, (err) => { 2044 if (err === undefined) { 2045 console.info('asset close succeed.'); 2046 } else { 2047 console.error('close failed, message = ' + err); 2048 } 2049 }); 2050 } catch (err) { 2051 console.error('close failed, message = ' + err); 2052 } 2053} 2054``` 2055 2056### close 2057 2058close(fd: number): Promise<void> 2059 2060Closes a file. This API uses a promise to return the result. 2061 2062**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2063 2064**Parameters** 2065 2066| Name | Type | Mandatory | Description | 2067| ---- | ------ | ---- | ----- | 2068| fd | number | Yes | FD of the file to close.| 2069 2070**Return value** 2071 2072| Type | Description | 2073| ------------------- | ---------- | 2074| Promise<void> | Promise that returns no value.| 2075 2076**Error codes** 2077 2078For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2079 2080| ID| Error Message| 2081| -------- | ---------------------------------------- | 2082| 401 | if parameter is invalid. | 2083| 13900020 | Invalid argument. | 2084| 14000011 | System inner fail. | 2085 2086**Example** 2087 2088```ts 2089import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2090 2091async function example() { 2092 console.info('closeDemo'); 2093 try { 2094 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2095 let fetchOption: photoAccessHelper.FetchOptions = { 2096 fetchColumns: [], 2097 predicates: predicates 2098 }; 2099 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2100 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 2101 let fd = await asset.open('rw'); 2102 console.info('file fd', fd); 2103 await asset.close(fd); 2104 console.info('asset close succeed.'); 2105 } catch (err) { 2106 console.error('close failed, message = ' + err); 2107 } 2108} 2109``` 2110 2111### getThumbnail 2112 2113getThumbnail(callback: AsyncCallback<image.PixelMap>): void 2114 2115Obtains the thumbnail of this file. This API uses an asynchronous callback to return the result. 2116 2117**Required permissions**: ohos.permission.READ_IMAGEVIDEO 2118 2119**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2120 2121**Parameters** 2122 2123| Name | Type | Mandatory | Description | 2124| -------- | ----------------------------------- | ---- | ---------------- | 2125| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | Yes | Callback invoked to return the PixelMap of the thumbnail.| 2126 2127**Error codes** 2128 2129For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2130 2131| ID| Error Message| 2132| -------- | ---------------------------------------- | 2133| 401 | if parameter is invalid. | 2134| 13900012 | Permission denied. | 2135| 13900020 | Invalid argument. | 2136| 14000011 | System inner fail. | 2137 2138**Example** 2139 2140```ts 2141import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2142 2143async function example() { 2144 console.info('getThumbnailDemo'); 2145 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2146 let fetchOption: photoAccessHelper.FetchOptions = { 2147 fetchColumns: [], 2148 predicates: predicates 2149 }; 2150 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2151 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 2152 console.info('asset displayName = ', asset.displayName); 2153 asset.getThumbnail((err, pixelMap) => { 2154 if (err === undefined) { 2155 console.info('getThumbnail successful ' + pixelMap); 2156 } else { 2157 console.error('getThumbnail fail', err); 2158 } 2159 }); 2160} 2161``` 2162 2163### getThumbnail 2164 2165getThumbnail(size: image.Size, callback: AsyncCallback<image.PixelMap>): void 2166 2167Obtains the file thumbnail of the given size. This API uses an asynchronous callback to return the result. 2168 2169**Required permissions**: ohos.permission.READ_IMAGEVIDEO 2170 2171**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2172 2173**Parameters** 2174 2175| Name | Type | Mandatory | Description | 2176| -------- | ----------------------------------- | ---- | ---------------- | 2177| size | [image.Size](js-apis-image.md#size) | Yes | Size of the thumbnail. | 2178| callback | AsyncCallback<[image.PixelMap](js-apis-image.md#pixelmap7)> | Yes | Callback invoked to return the PixelMap of the thumbnail.| 2179 2180**Error codes** 2181 2182For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2183 2184| ID| Error Message| 2185| -------- | ---------------------------------------- | 2186| 401 | if parameter is invalid. | 2187| 13900012 | Permission denied. | 2188| 13900020 | Invalid argument. | 2189| 14000011 | System inner fail. | 2190 2191**Example** 2192 2193```ts 2194import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2195import image from '@ohos.multimedia.image' 2196 2197async function example() { 2198 console.info('getThumbnailDemo'); 2199 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2200 let fetchOption: photoAccessHelper.FetchOptions = { 2201 fetchColumns: [], 2202 predicates: predicates 2203 }; 2204 let size: image.Size = { width: 720, height: 720 }; 2205 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2206 let asset = await fetchResult.getFirstObject(); 2207 console.info('asset displayName = ', asset.displayName); 2208 asset.getThumbnail(size, (err, pixelMap) => { 2209 if (err === undefined) { 2210 console.info('getThumbnail successful ' + pixelMap); 2211 } else { 2212 console.error('getThumbnail fail', err); 2213 } 2214 }); 2215} 2216``` 2217 2218### getThumbnail 2219 2220getThumbnail(size?: image.Size): Promise<image.PixelMap> 2221 2222Obtains the file thumbnail of the given size. This API uses a promise to return the result. 2223 2224**Required permissions**: ohos.permission.READ_IMAGEVIDEO 2225 2226**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2227 2228**Parameters** 2229 2230| Name | Type | Mandatory | Description | 2231| ---- | -------------- | ---- | ----- | 2232| size | [image.Size](js-apis-image.md#size) | No | Size of the thumbnail.| 2233 2234**Return value** 2235 2236| Type | Description | 2237| ----------------------------- | --------------------- | 2238| Promise<[image.PixelMap](js-apis-image.md#pixelmap7)> | Promise used to return the PixelMap of the thumbnail.| 2239 2240**Error codes** 2241 2242For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2243 2244| ID| Error Message| 2245| -------- | ---------------------------------------- | 2246| 401 | if parameter is invalid. | 2247| 13900012 | Permission denied. | 2248| 13900020 | Invalid argument. | 2249| 14000011 | System inner fail. | 2250 2251**Example** 2252 2253```ts 2254import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2255import image from '@ohos.multimedia.image' 2256import { BusinessError } from '@ohos.base'; 2257 2258async function example() { 2259 console.info('getThumbnailDemo'); 2260 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2261 let fetchOption: photoAccessHelper.FetchOptions = { 2262 fetchColumns: [], 2263 predicates: predicates 2264 }; 2265 let size: image.Size = { width: 720, height: 720 }; 2266 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2267 let asset = await fetchResult.getFirstObject(); 2268 console.info('asset displayName = ', asset.displayName); 2269 asset.getThumbnail(size).then((pixelMap) => { 2270 console.info('getThumbnail successful ' + pixelMap); 2271 }).catch((err: BusinessError) => { 2272 console.error('getThumbnail fail' + err); 2273 }); 2274} 2275``` 2276 2277### setFavorite 2278 2279setFavorite(favoriteState: boolean, callback: AsyncCallback<void>): void 2280 2281Favorites or unfavorites this file. This API uses an asynchronous callback to return the result. 2282 2283**System API**: This is a system API. 2284 2285**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 2286 2287**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2288 2289**Parameters** 2290 2291| Name | Type | Mandatory | Description | 2292| ---------- | ------------------------- | ---- | ---------------------------------- | 2293| favoriteState | boolean | Yes | Operation to perform. The value **true** means to favorite the file asset, and **false** means the opposite.| 2294| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 2295 2296**Error codes** 2297 2298For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2299 2300| ID| Error Message| 2301| -------- | ---------------------------------------- | 2302| 202 | Called by non-system application. | 2303| 401 | if parameter is invalid. | 2304| 13900012 | Permission denied. | 2305| 13900020 | Invalid argument. | 2306| 14000011 | System inner fail. | 2307 2308**Example** 2309 2310```ts 2311import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2312 2313async function example() { 2314 console.info('setFavoriteDemo'); 2315 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2316 let fetchOption: photoAccessHelper.FetchOptions = { 2317 fetchColumns: [], 2318 predicates: predicates 2319 }; 2320 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2321 let asset = await fetchResult.getFirstObject(); 2322 asset.setFavorite(true, (err) => { 2323 if (err === undefined) { 2324 console.info('favorite successfully'); 2325 } else { 2326 console.error('favorite failed with error:' + err); 2327 } 2328 }); 2329} 2330``` 2331 2332### setFavorite 2333 2334setFavorite(favoriteState: boolean): Promise<void> 2335 2336Favorites or unfavorites this file asset. This API uses a promise to return the result. 2337 2338**System API**: This is a system API. 2339 2340**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 2341 2342**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2343 2344**Parameters** 2345 2346| Name | Type | Mandatory | Description | 2347| ---------- | ------- | ---- | ---------------------------------- | 2348| favoriteState | boolean | Yes | Operation to perform. The value **true** means to favorite the file asset, and **false** means the opposite.| 2349 2350**Return value** 2351 2352| Type | Description | 2353| ------------------- | ---------- | 2354| Promise<void> | Promise that returns no value.| 2355 2356**Error codes** 2357 2358For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2359 2360| ID| Error Message| 2361| -------- | ---------------------------------------- | 2362| 202 | Called by non-system application. | 2363| 401 | if parameter is invalid. | 2364| 13900012 | Permission denied. | 2365| 13900020 | Invalid argument. | 2366| 14000011 | System inner fail. | 2367 2368**Example** 2369 2370```ts 2371import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2372import { BusinessError } from '@ohos.base'; 2373 2374async function example() { 2375 console.info('setFavoriteDemo'); 2376 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2377 let fetchOption: photoAccessHelper.FetchOptions = { 2378 fetchColumns: [], 2379 predicates: predicates 2380 }; 2381 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2382 let asset = await fetchResult.getFirstObject(); 2383 asset.setFavorite(true).then(() => { 2384 console.info('setFavorite successfully'); 2385 }).catch((err: BusinessError) => { 2386 console.error('setFavorite failed with error:' + err); 2387 }); 2388} 2389``` 2390 2391### setHidden 2392 2393setHidden(hiddenState: boolean, callback: AsyncCallback<void>): void 2394 2395Sets this file to hidden state. This API uses an asynchronous callback to return the result. 2396 2397Private files are stored in the private album. After obtaining private files from the private album, users can set **hiddenState** to **false** to remove them from the private album. 2398 2399**System API**: This is a system API. 2400 2401**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 2402 2403**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2404 2405**Parameters** 2406 2407| Name | Type | Mandatory | Description | 2408| ---------- | ------------------------- | ---- | ---------------------------------- | 2409| hiddenState | boolean | Yes | Whether to set a file to hidden state. The value **true** means to hide the file; the value **false** means the opposite.| 2410| callback | AsyncCallback<void> | Yes | Callback that returns no value. | 2411 2412**Error codes** 2413 2414For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2415 2416| ID| Error Message| 2417| -------- | ---------------------------------------- | 2418| 202 | Called by non-system application. | 2419| 401 | if parameter is invalid. | 2420| 13900012 | Permission denied. | 2421| 13900020 | Invalid argument. | 2422| 14000011 | System inner fail. | 2423 2424**Example** 2425 2426```ts 2427import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2428 2429async function example() { 2430 console.info('setHiddenDemo'); 2431 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2432 let fetchOption: photoAccessHelper.FetchOptions = { 2433 fetchColumns: [], 2434 predicates: predicates 2435 }; 2436 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2437 let asset = await fetchResult.getFirstObject(); 2438 asset.setHidden(true, (err) => { 2439 if (err === undefined) { 2440 console.info('setHidden successfully'); 2441 } else { 2442 console.error('setHidden failed with error:' + err); 2443 } 2444 }); 2445} 2446``` 2447 2448### setHidden 2449 2450setHidden(hiddenState: boolean): Promise<void> 2451 2452Sets this file asset to hidden state. This API uses a promise to return the result. 2453 2454Private files are stored in the private album. After obtaining private files from the private album, users can set **hiddenState** to **false** to remove them from the private album. 2455 2456**System API**: This is a system API. 2457 2458**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 2459 2460**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2461 2462**Parameters** 2463 2464| Name | Type | Mandatory | Description | 2465| ---------- | ------- | ---- | ---------------------------------- | 2466| hiddenState | boolean | Yes | Whether to set a file to hidden state. The value **true** means to hide the file; the value **false** means the opposite.| 2467 2468**Return value** 2469 2470| Type | Description | 2471| ------------------- | ---------- | 2472| Promise<void> | Promise that returns no value.| 2473 2474**Error codes** 2475 2476For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2477 2478| ID| Error Message| 2479| -------- | ---------------------------------------- | 2480| 202 | Called by non-system application. | 2481| 401 | if parameter is invalid. | 2482| 13900012 | Permission denied. | 2483| 13900020 | Invalid argument. | 2484| 14000011 | System inner fail. | 2485 2486**Example** 2487 2488```ts 2489import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2490import { BusinessError } from '@ohos.base'; 2491 2492async function example() { 2493 // Restore a file from a hidden album. Before the operation, ensure that the file exists in the hidden album. 2494 console.info('setHiddenDemo'); 2495 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2496 let fetchOption: photoAccessHelper.FetchOptions = { 2497 fetchColumns: [], 2498 predicates: predicates 2499 }; 2500 let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.HIDDEN); 2501 let album = await albumList.getFirstObject(); 2502 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 2503 let asset = await fetchResult.getFirstObject(); 2504 asset.setHidden(false).then(() => { 2505 console.info('setHidden successfully'); 2506 }).catch((err: BusinessError) => { 2507 console.error('setHidden failed with error:' + err); 2508 }); 2509} 2510``` 2511 2512### getExif 2513 2514getExif(): Promise<string> 2515 2516Obtains a JSON string consisting of the EXIF tags of this JPG image. This API uses a promise to return the result. 2517 2518The EXIF tag information is provided by the [image](js-apis-image.md) module. For details about the EXIF tag information, see [image.PropertyKey](js-apis-image.md#propertykey7). 2519 2520> **NOTE**<br>This API returns a JSON string consisting of EXIF tags. The complete EXIF information consists of **all_exif** and [PhotoKeys.USER_COMMENT](#photokeys). These two fields must be passed in via **fetchColumns**. 2521 2522**System API**: This is a system API. 2523 2524**Required permissions**: ohos.permission.READ_IMAGEVIDEO 2525 2526**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2527 2528**Return value** 2529 2530| Type | Description | 2531| --------------------------------------- | ----------------- | 2532| Promise<string> | Callback invoked to return the JSON string obtained.| 2533 2534**Error codes** 2535 2536For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2537 2538| ID| Error Message| 2539| -------- | ---------------------------------------- | 2540| 202 | Called by non-system application. | 2541| 401 | if parameter is invalid. | 2542| 13900012 | Permission denied. | 2543| 13900020 | Invalid argument. | 2544| 14000011 | System inner fail. | 2545 2546**Example** 2547 2548```ts 2549import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2550 2551async function example() { 2552 try { 2553 console.info('getExifDemo'); 2554 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2555 let fetchOptions: photoAccessHelper.FetchOptions = { 2556 fetchColumns: [ 'all_exif', photoAccessHelper.PhotoKeys.USER_COMMENT], 2557 predicates: predicates 2558 }; 2559 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 2560 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 2561 let exifMessage = await photoAsset.getExif(); 2562 let userCommentKey = 'UserComment'; 2563 let userComment = JSON.stringify(JSON.parse(exifMessage), [userCommentKey]); 2564 fetchResult.close(); 2565 } catch (err) { 2566 console.error('getExifDemoCallback failed with error: ' + err); 2567 } 2568} 2569``` 2570 2571### getExif 2572 2573getExif(callback: AsyncCallback<string>): void 2574 2575Obtains a JSON string consisting of the EXIF tags of this JPG image. This API uses an asynchronous callback to return the result. 2576 2577The EXIF tag information is provided by the [image](js-apis-image.md) module. For details about the EXIF tag information, see [image.PropertyKey](js-apis-image.md#propertykey7). 2578 2579> **NOTE**<br>This API returns a JSON string consisting of EXIF tags. The complete EXIF information consists of **all_exif** and [PhotoKeys.USER_COMMENT](#photokeys). These two fields must be passed in via **fetchColumns**. 2580 2581**System API**: This is a system API. 2582 2583**Required permissions**: ohos.permission.READ_IMAGEVIDEO 2584 2585**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2586 2587**Parameters** 2588 2589| Name | Type | Mandatory| Description | 2590| -------- | ------------------------- | ---- | ---------- | 2591| callback | AsyncCallback<string> | Yes | Callback invoked to return the JSON string obtained.| 2592 2593**Error codes** 2594 2595For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2596 2597| ID| Error Message| 2598| -------- | ---------------------------------------- | 2599| 202 | Called by non-system application. | 2600| 401 | if parameter is invalid. | 2601| 13900012 | Permission denied. | 2602| 13900020 | Invalid argument. | 2603| 14000011 | System inner fail. | 2604 2605**Example** 2606 2607```ts 2608import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2609 2610async function example() { 2611 try { 2612 console.info('getExifDemo'); 2613 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2614 predicates.isNotNull('all_exif') 2615 let fetchOptions: photoAccessHelper.FetchOptions = { 2616 fetchColumns: ['all_exif', photoAccessHelper.PhotoKeys.USER_COMMENT], 2617 predicates: predicates 2618 }; 2619 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 2620 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 2621 console.info('getExifDemo photoAsset displayName: ' + JSON.stringify(photoAsset.displayName)); 2622 let userCommentKey = 'UserComment'; 2623 photoAsset.getExif((err, exifMessage) => { 2624 if (exifMessage !== undefined) { 2625 let userComment = JSON.stringify(JSON.parse(exifMessage), [userCommentKey]); 2626 console.info('getExifDemo userComment: ' + JSON.stringify(userComment)); 2627 } else { 2628 console.error('getExif failed, message = ', err); 2629 } 2630 }); 2631 fetchResult.close(); 2632 } catch (err) { 2633 console.error('getExifDemoCallback failed with error: ' + err); 2634 } 2635} 2636``` 2637 2638### setUserComment 2639 2640setUserComment(userComment: string): Promise<void> 2641 2642Sets user comment information of an image or video. This API uses a promise to return the result. 2643 2644> **NOTE**<br>This API can be used to modify the comment information of only images or videos. 2645 2646**System API**: This is a system API. 2647 2648**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 2649 2650**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2651 2652**Parameters** 2653 2654| Name | Type | Mandatory| Description | 2655| -------- | ------------------------- | ---- | ---------- | 2656| userComment | string | Yes | User comment information to set, which cannot exceed 140 characters.| 2657 2658**Return value** 2659 2660| Type | Description | 2661| --------------------------------------- | ----------------- | 2662|Promise<void> | Promise that returns no value.| 2663 2664**Error codes** 2665 2666For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2667 2668| ID| Error Message| 2669| -------- | ---------------------------------------- | 2670| 202 | Called by non-system application. | 2671| 401 | if parameter is invalid. | 2672| 13900012 | Permission denied. | 2673| 13900020 | Invalid argument. | 2674| 14000011 | System inner fail. | 2675 2676**Example** 2677 2678```ts 2679import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2680 2681async function example() { 2682 try { 2683 console.info('setUserCommentDemo') 2684 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2685 let fetchOptions: photoAccessHelper.FetchOptions = { 2686 fetchColumns: [], 2687 predicates: predicates 2688 }; 2689 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 2690 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 2691 let userComment = 'test_set_user_comment'; 2692 await photoAsset.setUserComment(userComment); 2693 } catch (err) { 2694 console.error('setUserCommentDemoCallback failed with error: ' + err); 2695 } 2696} 2697``` 2698 2699### setUserComment 2700 2701setUserComment(userComment: string, callback: AsyncCallback<void>): void 2702 2703Sets user comment information of an image or video. This API uses an asynchronous callback to return the result. 2704 2705> **NOTE**<br>This API can be used to modify the comment information of only images or videos. 2706 2707**System API**: This is a system API. 2708 2709**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 2710 2711**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2712 2713**Parameters** 2714 2715| Name | Type | Mandatory| Description | 2716| -------- | ------------------------- | ---- | ---------- | 2717| userComment | string | Yes | User comment information to set, which cannot exceed 140 characters.| 2718| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 2719 2720**Error codes** 2721 2722For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2723 2724| ID| Error Message| 2725| -------- | ---------------------------------------- | 2726| 202 | Called by non-system application. | 2727| 401 | if parameter is invalid. | 2728| 13900012 | Permission denied. | 2729| 13900020 | Invalid argument. | 2730| 14000011 | System inner fail. | 2731 2732**Example** 2733 2734```ts 2735import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2736 2737async function example() { 2738 try { 2739 console.info('setUserCommentDemo') 2740 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2741 let fetchOptions: photoAccessHelper.FetchOptions = { 2742 fetchColumns: [], 2743 predicates: predicates 2744 }; 2745 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 2746 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 2747 let userComment = 'test_set_user_comment'; 2748 photoAsset.setUserComment(userComment, (err) => { 2749 if (err === undefined) { 2750 console.info('setUserComment successfully'); 2751 } else { 2752 console.error('setUserComment failed with error: ' + err); 2753 } 2754 }); 2755 } catch (err) { 2756 console.error('setUserCommentDemoCallback failed with error: ' + err); 2757 } 2758} 2759``` 2760 2761## PhotoViewPicker 2762 2763Provides APIs for the user to select images and videos. Before using the APIs of **PhotoViewPicker**, you need to create a **PhotoViewPicker** instance. 2764 2765**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2766 2767**Example** 2768 2769```ts 2770let photoPicker = new photoAccessHelper.PhotoViewPicker(); 2771``` 2772 2773### select 2774 2775select(option?: PhotoSelectOptions) : Promise<PhotoSelectResult> 2776 2777Starts a **photoPicker** page for the user to select one or more images or videos. This API uses a promise to return the result. You can pass in **PhotoSelectOptions** to specify the media file type and the maximum number of files to select. 2778 2779> **NOTE**<br>The **photoUris** in the **PhotoSelectResult** object returned by this API can be used only by calling [photoAccessHelper.getAssets()](#getassets) with temporary authorization. For details, see [Using a Media File URI](../../file-management/user-file-uri-intro.md#using-a-media-file-uri). 2780 2781**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2782 2783**Parameters** 2784 2785| Name | Type | Mandatory| Description | 2786| ------- | ------- | ---- | -------------------------- | 2787| option | [PhotoSelectOptions](#photoselectoptions) | No | Options for selecting files. If this parameter is not specified, images and videos are selected by default. A maximum of 50 files can be selected.| 2788 2789**Return value** 2790 2791| Type | Description | 2792| ----------------------------- | :---- | 2793| Promise<[PhotoSelectResult](#photoselectresult)> | Promise return information about the images or videos selected.| 2794 2795**Error codes** 2796 2797For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2798 2799| ID| Error Message| 2800| -------- | ---------------------------------------- | 2801| 401 | if parameter is invalid. | 2802| 13900042 | Unknown error. | 2803 2804**Example** 2805 2806```ts 2807import { BusinessError } from '@ohos.base'; 2808async function example01() { 2809 try { 2810 let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); 2811 PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; 2812 PhotoSelectOptions.maxSelectNumber = 5; 2813 let photoPicker = new photoAccessHelper.PhotoViewPicker(); 2814 photoPicker.select(PhotoSelectOptions).then((PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => { 2815 console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult)); 2816 }).catch((err: BusinessError) => { 2817 console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err)); 2818 }); 2819 } catch (error) { 2820 let err: BusinessError = error as BusinessError; 2821 console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err)); 2822 } 2823} 2824``` 2825 2826### select 2827 2828select(option: PhotoSelectOptions, callback: AsyncCallback<PhotoSelectResult>) : void 2829 2830Starts a **photoPicker** page for the user to select one or more images or videos. This API uses an asynchronous callback to return the result. You can pass in **PhotoSelectOptions** to specify the media file type and the maximum number of files to select. 2831 2832> **NOTE**<br>The **photoUris** in the **PhotoSelectResult** object returned by this API can be used only by calling [photoAccessHelper.getAssets()](#getassets) with temporary authorization. For details, see [Using a Media File URI](../../file-management/user-file-uri-intro.md#using-a-media-file-uri). 2833 2834**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2835 2836**Parameters** 2837 2838| Name | Type | Mandatory| Description | 2839| ------- | ------- | ---- | -------------------------- | 2840| option | [PhotoSelectOptions](#photoselectoptions) | Yes | Options for selecting images or videos.| 2841| callback | AsyncCallback<[PhotoSelectResult](#photoselectresult)> | Yes | Callback invoked to return information about the images or videos selected.| 2842 2843**Error codes** 2844 2845For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2846 2847| ID| Error Message| 2848| -------- | ---------------------------------------- | 2849| 401 | if parameter is invalid. | 2850| 13900042 | Unknown error. | 2851 2852**Example** 2853 2854```ts 2855import { BusinessError } from '@ohos.base'; 2856async function example02() { 2857 try { 2858 let PhotoSelectOptions = new photoAccessHelper.PhotoSelectOptions(); 2859 PhotoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE; 2860 PhotoSelectOptions.maxSelectNumber = 5; 2861 let photoPicker = new photoAccessHelper.PhotoViewPicker(); 2862 photoPicker.select(PhotoSelectOptions, (err: BusinessError, PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => { 2863 if (err) { 2864 console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err)); 2865 return; 2866 } 2867 console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult)); 2868 }); 2869 } catch (error) { 2870 let err: BusinessError = error as BusinessError; 2871 console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err)); 2872 } 2873} 2874``` 2875 2876### select 2877 2878select(callback: AsyncCallback<PhotoSelectResult>) : void 2879 2880Starts a **photoPicker** page for the user to select one or more images or videos. This API uses an asynchronous callback to return the result. 2881 2882> **NOTE**<br>The **photoUris** in the **PhotoSelectResult** object returned by this API can be used only by calling [photoAccessHelper.getAssets()](#getassets) with temporary authorization. For details, see [Using a Media File URI](../../file-management/user-file-uri-intro.md#using-a-media-file-uri). 2883 2884**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2885 2886**Parameters** 2887 2888| Name | Type | Mandatory| Description | 2889| ------- | ------- | ---- | -------------------------- | 2890| callback | AsyncCallback<[PhotoSelectResult](#photoselectresult)> | Yes | Callback invoked to return information about the images or videos selected.| 2891 2892**Error codes** 2893 2894For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2895 2896| ID| Error Message| 2897| -------- | ---------------------------------------- | 2898| 401 | if parameter is invalid. | 2899| 13900042 | Unknown error. | 2900 2901**Example** 2902 2903```ts 2904import { BusinessError } from '@ohos.base'; 2905async function example03() { 2906 try { 2907 let photoPicker = new photoAccessHelper.PhotoViewPicker(); 2908 photoPicker.select((err: BusinessError, PhotoSelectResult: photoAccessHelper.PhotoSelectResult) => { 2909 if (err) { 2910 console.error('PhotoViewPicker.select failed with err: ' + JSON.stringify(err)); 2911 return; 2912 } 2913 console.info('PhotoViewPicker.select successfully, PhotoSelectResult uri: ' + JSON.stringify(PhotoSelectResult)); 2914 }); 2915 } catch (error) { 2916 let err: BusinessError = error as BusinessError; 2917 console.error('PhotoViewPicker failed with err: ' + JSON.stringify(err)); 2918 } 2919} 2920``` 2921 2922## FetchResult 2923 2924Provides APIs to manage the file retrieval result. 2925 2926### getCount 2927 2928getCount(): number 2929 2930Obtains the total number of files in the result set. 2931 2932**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2933 2934**Return value** 2935 2936| Type | Description | 2937| ------ | -------- | 2938| number | Returns the total number of files obtained.| 2939 2940**Error codes** 2941 2942For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2943 2944| ID| Error Message| 2945| -------- | ---------------------------------------- | 2946| 401 | if parameter is invalid. | 2947| 13900020 | Invalid argument. | 2948| 14000011 | System inner fail. | 2949 2950**Example** 2951 2952```ts 2953import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2954 2955async function example() { 2956 console.info('getCountDemo'); 2957 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2958 let fetchOption: photoAccessHelper.FetchOptions = { 2959 fetchColumns: [], 2960 predicates: predicates 2961 }; 2962 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 2963 let fetchCount = fetchResult.getCount(); 2964 console.info('fetchCount = ', fetchCount); 2965} 2966``` 2967 2968### isAfterLast 2969 2970isAfterLast(): boolean 2971 2972Checks whether the cursor is in the last row of the result set. 2973 2974**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 2975 2976**Return value** 2977 2978| Type | Description | 2979| ------- | ---------------------------------- | 2980| boolean | Returns **true** if the cursor is in the last row of the result set; returns **false** otherwise.| 2981 2982**Error codes** 2983 2984For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 2985 2986| ID| Error Message| 2987| -------- | ---------------------------------------- | 2988| 401 | if parameter is invalid. | 2989| 13900020 | Invalid argument. | 2990| 14000011 | System inner fail. | 2991 2992**Example** 2993 2994```ts 2995import dataSharePredicates from '@ohos.data.dataSharePredicates'; 2996 2997async function example() { 2998 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 2999 let fetchOption: photoAccessHelper.FetchOptions = { 3000 fetchColumns: [], 3001 predicates: predicates 3002 }; 3003 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3004 let fetchCount = fetchResult.getCount(); 3005 console.info('count:' + fetchCount); 3006 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); 3007 if (fetchResult.isAfterLast()) { 3008 console.info('photoAsset isAfterLast displayName = ', photoAsset.displayName); 3009 } else { 3010 console.info('photoAsset not isAfterLast '); 3011 } 3012} 3013``` 3014 3015### close 3016 3017close(): void 3018 3019Closes this **FetchFileResult** instance to invalidate it. After this instance is closed, the APIs in this instance cannot be invoked. 3020 3021**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3022 3023**Error codes** 3024 3025For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3026 3027| ID| Error Message| 3028| -------- | ---------------------------------------- | 3029| 401 | if parameter is invalid. | 3030| 13900020 | Invalid argument. | 3031| 14000011 | System inner fail. | 3032 3033**Example** 3034 3035```ts 3036import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3037 3038async function example() { 3039 console.info('fetchResultCloseDemo'); 3040 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3041 let fetchOption: photoAccessHelper.FetchOptions = { 3042 fetchColumns: [], 3043 predicates: predicates 3044 }; 3045 try { 3046 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3047 fetchResult.close(); 3048 console.info('close succeed.'); 3049 } catch (err) { 3050 console.error('close fail. message = ' + err); 3051 } 3052} 3053``` 3054 3055### getFirstObject 3056 3057getFirstObject(callback: AsyncCallback<T>): void 3058 3059Obtains the first file asset in the result set. This API uses an asynchronous callback to return the result. 3060 3061**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3062 3063**Parameters** 3064 3065| Name | Type | Mandatory| Description | 3066| -------- | --------------------------------------------- | ---- | ------------------------------------------- | 3067| callback | AsyncCallback<T> | Yes | Callback invoked to return the first file asset obtained.| 3068 3069**Error codes** 3070 3071For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3072 3073| ID| Error Message| 3074| -------- | ---------------------------------------- | 3075| 401 | if parameter is invalid. | 3076| 13900020 | Invalid argument. | 3077| 14000011 | System inner fail. | 3078 3079**Example** 3080 3081```ts 3082import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3083 3084async function example() { 3085 console.info('getFirstObjectDemo'); 3086 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3087 let fetchOption: photoAccessHelper.FetchOptions = { 3088 fetchColumns: [], 3089 predicates: predicates 3090 }; 3091 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3092 fetchResult.getFirstObject((err, photoAsset) => { 3093 if (photoAsset !== undefined) { 3094 console.info('photoAsset displayName: ', photoAsset.displayName); 3095 } else { 3096 console.error('photoAsset failed with err:' + err); 3097 } 3098 }); 3099} 3100``` 3101 3102### getFirstObject 3103 3104getFirstObject(): Promise<T> 3105 3106Obtains the first file asset in the result set. This API uses a promise to return the result. 3107 3108**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3109 3110**Return value** 3111 3112| Type | Description | 3113| --------------------------------------- | -------------------------- | 3114| Promise<T> | Promise used to return the first object in the result set.| 3115 3116**Error codes** 3117 3118For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3119 3120| ID| Error Message| 3121| -------- | ---------------------------------------- | 3122| 401 | if parameter is invalid. | 3123| 13900020 | Invalid argument. | 3124| 14000011 | System inner fail. | 3125 3126**Example** 3127 3128```ts 3129import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3130 3131async function example() { 3132 console.info('getFirstObjectDemo'); 3133 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3134 let fetchOption: photoAccessHelper.FetchOptions = { 3135 fetchColumns: [], 3136 predicates: predicates 3137 }; 3138 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3139 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 3140 console.info('photoAsset displayName: ', photoAsset.displayName); 3141} 3142``` 3143 3144### getNextObject 3145 3146getNextObject(callback: AsyncCallback<T>): void 3147 3148Obtains the next file asset in the result set. This API uses an asynchronous callback to return the result. 3149Before using this API, you must use [isAfterLast()](#isafterlast) to check whether the current position is the end of the result set. 3150 3151**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3152 3153**Parameters** 3154 3155| Name | Type | Mandatory| Description | 3156| --------- | --------------------------------------------- | ---- | ----------------------------------------- | 3157| callback | AsyncCallback<T> | Yes | Callback invoked to return the next file asset.| 3158 3159**Error codes** 3160 3161For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3162 3163| ID| Error Message| 3164| -------- | ---------------------------------------- | 3165| 401 | if parameter is invalid. | 3166| 13900020 | Invalid argument. | 3167| 14000011 | System inner fail. | 3168 3169**Example** 3170 3171```ts 3172import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3173 3174async function example() { 3175 console.info('getNextObjectDemo'); 3176 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3177 let fetchOption: photoAccessHelper.FetchOptions = { 3178 fetchColumns: [], 3179 predicates: predicates 3180 }; 3181 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3182 await fetchResult.getFirstObject(); 3183 if (!fetchResult.isAfterLast()) { 3184 fetchResult.getNextObject((err, photoAsset) => { 3185 if (photoAsset !== undefined) { 3186 console.info('photoAsset displayName: ', photoAsset.displayName); 3187 } else { 3188 console.error('photoAsset failed with err: ' + err); 3189 } 3190 }); 3191 } 3192} 3193``` 3194 3195### getNextObject 3196 3197getNextObject(): Promise<T> 3198 3199Obtains the next file asset in the result set. This API uses a promise to return the result. 3200Before using this API, you must use [isAfterLast()](#isafterlast) to check whether the current position is the end of the result set. 3201 3202**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3203 3204**Return value** 3205 3206| Type | Description | 3207| --------------------------------------- | ----------------- | 3208| Promise<T> | Promise used to return the next object in the result set.| 3209 3210**Error codes** 3211 3212For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3213 3214| ID| Error Message| 3215| -------- | ---------------------------------------- | 3216| 401 | if parameter is invalid. | 3217| 13900020 | Invalid argument. | 3218| 14000011 | System inner fail. | 3219 3220**Example** 3221 3222```ts 3223import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3224 3225async function example() { 3226 console.info('getNextObjectDemo'); 3227 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3228 let fetchOption: photoAccessHelper.FetchOptions = { 3229 fetchColumns: [], 3230 predicates: predicates 3231 }; 3232 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3233 await fetchResult.getFirstObject(); 3234 if (!fetchResult.isAfterLast()) { 3235 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getNextObject(); 3236 console.info('photoAsset displayName: ', photoAsset.displayName); 3237 } 3238} 3239``` 3240 3241### getLastObject 3242 3243getLastObject(callback: AsyncCallback<T>): void 3244 3245Obtains the last file asset in the result set. This API uses an asynchronous callback to return the result. 3246 3247**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3248 3249**Parameters** 3250 3251| Name | Type | Mandatory| Description | 3252| -------- | --------------------------------------------- | ---- | --------------------------- | 3253| callback | AsyncCallback<T> | Yes | Callback invoked to return the last file asset obtained.| 3254 3255**Error codes** 3256 3257For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3258 3259| ID| Error Message| 3260| -------- | ---------------------------------------- | 3261| 401 | if parameter is invalid. | 3262| 13900020 | Invalid argument. | 3263| 14000011 | System inner fail. | 3264 3265**Example** 3266 3267```ts 3268import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3269 3270async function example() { 3271 console.info('getLastObjectDemo'); 3272 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3273 let fetchOption: photoAccessHelper.FetchOptions = { 3274 fetchColumns: [], 3275 predicates: predicates 3276 }; 3277 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3278 fetchResult.getLastObject((err, photoAsset) => { 3279 if (photoAsset !== undefined) { 3280 console.info('photoAsset displayName: ', photoAsset.displayName); 3281 } else { 3282 console.error('photoAsset failed with err: ' + err); 3283 } 3284 }); 3285} 3286``` 3287 3288### getLastObject 3289 3290getLastObject(): Promise<T> 3291 3292Obtains the last file asset in the result set. This API uses a promise to return the result. 3293 3294**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3295 3296**Return value** 3297 3298| Type | Description | 3299| --------------------------------------- | ----------------- | 3300| Promise<T> | Promise used to return the last object in the result set.| 3301 3302**Error codes** 3303 3304For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3305 3306| ID| Error Message| 3307| -------- | ---------------------------------------- | 3308| 401 | if parameter is invalid. | 3309| 13900020 | Invalid argument. | 3310| 14000011 | System inner fail. | 3311 3312**Example** 3313 3314```ts 3315import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3316 3317async function example() { 3318 console.info('getLastObjectDemo'); 3319 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3320 let fetchOption: photoAccessHelper.FetchOptions = { 3321 fetchColumns: [], 3322 predicates: predicates 3323 }; 3324 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3325 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); 3326 console.info('photoAsset displayName: ', photoAsset.displayName); 3327} 3328``` 3329 3330### getObjectByPosition 3331 3332getObjectByPosition(index: number, callback: AsyncCallback<T>): void 3333 3334Obtains a file asset with the specified index in the result set. This API uses an asynchronous callback to return the result. 3335 3336**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3337 3338**Parameters** 3339 3340| Name | Type | Mandatory | Description | 3341| -------- | ---------------------------------------- | ---- | ------------------ | 3342| index | number | Yes | Index of the file asset to obtain. The value starts from **0**. | 3343| callback | AsyncCallback<T> | Yes | Callback invoked to return the file asset obtained.| 3344 3345**Error codes** 3346 3347For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3348 3349| ID| Error Message| 3350| -------- | ---------------------------------------- | 3351| 401 | if parameter is invalid. | 3352| 13900020 | Invalid argument. | 3353| 14000011 | System inner fail. | 3354 3355**Example** 3356 3357```ts 3358import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3359 3360async function example() { 3361 console.info('getObjectByPositionDemo'); 3362 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3363 let fetchOption: photoAccessHelper.FetchOptions = { 3364 fetchColumns: [], 3365 predicates: predicates 3366 }; 3367 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3368 fetchResult.getObjectByPosition(0, (err, photoAsset) => { 3369 if (photoAsset !== undefined) { 3370 console.info('photoAsset displayName: ', photoAsset.displayName); 3371 } else { 3372 console.error('photoAsset failed with err: ' + err); 3373 } 3374 }); 3375} 3376``` 3377 3378### getObjectByPosition 3379 3380getObjectByPosition(index: number): Promise<T> 3381 3382Obtains a file asset with the specified index in the result set. This API uses a promise to return the result. 3383 3384**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3385 3386**Parameters** 3387 3388| Name | Type | Mandatory | Description | 3389| ----- | ------ | ---- | -------------- | 3390| index | number | Yes | Index of the file asset to obtain. The value starts from **0**.| 3391 3392**Return value** 3393 3394| Type | Description | 3395| --------------------------------------- | ----------------- | 3396| Promise<T> | Promise used to return the file asset obtained.| 3397 3398**Error codes** 3399 3400For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3401 3402| ID| Error Message| 3403| -------- | ---------------------------------------- | 3404| 401 | if parameter is invalid. | 3405| 13900020 | Invalid argument. | 3406| 14000011 | System inner fail. | 3407 3408**Example** 3409 3410```ts 3411import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3412 3413async function example() { 3414 console.info('getObjectByPositionDemo'); 3415 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3416 let fetchOption: photoAccessHelper.FetchOptions = { 3417 fetchColumns: [], 3418 predicates: predicates 3419 }; 3420 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3421 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getObjectByPosition(0); 3422 console.info('photoAsset displayName: ', photoAsset.displayName); 3423} 3424``` 3425 3426### getAllObjects 3427 3428getAllObjects(callback: AsyncCallback<Array<T>>): void 3429 3430Obtains all the file assets in the result set. This API uses an asynchronous callback to return the result. 3431 3432**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3433 3434**Parameters** 3435 3436| Name | Type | Mandatory| Description | 3437| -------- | --------------------------------------------- | ---- | ------------------------------------------- | 3438| callback | AsyncCallback<Array<T>> | Yes | Callback invoked to return an array of all file assets in the result set.| 3439 3440**Error codes** 3441 3442For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3443 3444| ID| Error Message| 3445| -------- | ---------------------------------------- | 3446| 401 | if parameter is invalid. | 3447| 13900020 | Invalid argument. | 3448| 14000011 | System inner fail. | 3449 3450**Example** 3451 3452```ts 3453import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3454 3455async function example() { 3456 console.info('getAllObjectDemo'); 3457 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3458 let fetchOption: photoAccessHelper.FetchOptions = { 3459 fetchColumns: [], 3460 predicates: predicates 3461 }; 3462 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3463 fetchResult.getAllObjects((err, photoAssetList) => { 3464 if (photoAssetList !== undefined) { 3465 console.info('photoAssetList length: ', photoAssetList.length); 3466 } else { 3467 console.error('photoAssetList failed with err:' + err); 3468 } 3469 }); 3470} 3471``` 3472 3473### getAllObjects 3474 3475getAllObjects(): Promise<Array<T>> 3476 3477Obtains all the file assets in the result set. This API uses a promise to return the result. 3478 3479**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3480 3481**Return value** 3482 3483| Type | Description | 3484| --------------------------------------- | -------------------------- | 3485| Promise<Array<T>> | Promise used to return an array of all file assets in the result set.| 3486 3487**Error codes** 3488 3489For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3490 3491| ID| Error Message| 3492| -------- | ---------------------------------------- | 3493| 401 | if parameter is invalid. | 3494| 13900020 | Invalid argument. | 3495| 14000011 | System inner fail. | 3496 3497**Example** 3498 3499```ts 3500import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3501 3502async function example() { 3503 console.info('getAllObjectDemo'); 3504 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3505 let fetchOption: photoAccessHelper.FetchOptions = { 3506 fetchColumns: [], 3507 predicates: predicates 3508 }; 3509 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3510 let photoAssetList: Array<photoAccessHelper.PhotoAsset> = await fetchResult.getAllObjects(); 3511 console.info('photoAssetList length: ', photoAssetList.length); 3512} 3513``` 3514 3515## Album 3516 3517Provides APIs to manage albums. 3518 3519### Attributes 3520 3521**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3522 3523| Name | Type | Readable | Writable | Description | 3524| ------------ | ------ | ---- | ---- | ------- | 3525| albumType | [AlbumType]( #albumtype) | Yes | No | Type of the album. | 3526| albumSubtype | [AlbumSubtype]( #albumsubtype) | Yes | No | Subtype of the album. | 3527| albumName | string | Yes | Yes for a user album; no for a system album. | Name of the album. | 3528| albumUri | string | Yes | No | URI of the album. | 3529| count | number | Yes | No | Number of files in the album.| 3530| coverUri | string | Yes | No | URI of the cover file of the album.| 3531 3532### getAssets 3533 3534getAssets(options: FetchOptions, callback: AsyncCallback<FetchResult<PhotoAsset>>): void 3535 3536Obtains image and video assets. This API uses an asynchronous callback to return the result. 3537 3538**Required permissions**: ohos.permission.READ_IMAGEVIDEO 3539 3540**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3541 3542**Parameters** 3543 3544| Name | Type | Mandatory| Description | 3545| -------- | ------------------------- | ---- | ---------- | 3546| options | [FetchOptions](#fetchoptions) | Yes | Options for fetching the albums.| 3547| callback | AsyncCallback<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Yes | Callback invoked to return the image and video assets obtained.| 3548 3549**Error codes** 3550 3551For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3552 3553| ID| Error Message| 3554| -------- | ---------------------------------------- | 3555| 401 | if parameter is invalid. | 3556| 13900012 | Permission denied. | 3557| 13900020 | Invalid argument. | 3558| 14000011 | System inner fail. | 3559 3560**Example** 3561 3562```ts 3563import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3564 3565async function example() { 3566 console.info('albumGetAssetsDemoCallback'); 3567 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3568 let albumFetchOptions: photoAccessHelper.FetchOptions = { 3569 fetchColumns: [], 3570 predicates: predicates 3571 }; 3572 let fetchOption: photoAccessHelper.FetchOptions = { 3573 fetchColumns: [], 3574 predicates: predicates 3575 }; 3576 let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 3577 let album: photoAccessHelper.Album = await albumList.getFirstObject(); 3578 album.getAssets(fetchOption, (err, albumFetchResult) => { 3579 if (albumFetchResult !== undefined) { 3580 console.info('album getAssets successfully, getCount: ' + albumFetchResult.getCount()); 3581 } else { 3582 console.error('album getAssets failed with error: ' + err); 3583 } 3584 }); 3585} 3586``` 3587 3588### getAssets 3589 3590getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 3591 3592Obtains image and video assets. This API uses a promise to return the result. 3593 3594**Required permissions**: ohos.permission.READ_IMAGEVIDEO 3595 3596**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3597 3598**Parameters** 3599 3600| Name | Type | Mandatory| Description | 3601| -------- | ------------------------- | ---- | ---------- | 3602| options | [FetchOptions](#fetchoptions) | Yes | Options for fetching the album files.| 3603 3604**Return value** 3605 3606| Type | Description | 3607| --------------------------------------- | ----------------- | 3608| Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise used to return the image and video assets obtained.| 3609 3610**Error codes** 3611 3612For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3613 3614| ID| Error Message| 3615| -------- | ---------------------------------------- | 3616| 401 | if parameter is invalid. | 3617| 13900012 | Permission denied. | 3618| 13900020 | Invalid argument. | 3619| 14000011 | System inner fail. | 3620 3621**Example** 3622 3623```ts 3624import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3625import { BusinessError } from '@ohos.base'; 3626 3627async function example() { 3628 console.info('albumGetAssetsDemoPromise'); 3629 3630 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3631 let albumFetchOptions: photoAccessHelper.FetchOptions = { 3632 fetchColumns: [], 3633 predicates: predicates 3634 }; 3635 let fetchOption: photoAccessHelper.FetchOptions = { 3636 fetchColumns: [], 3637 predicates: predicates 3638 }; 3639 let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 3640 let album: photoAccessHelper.Album = await albumList.getFirstObject(); 3641 album.getAssets(fetchOption).then((albumFetchResult) => { 3642 console.info('album getPhotoAssets successfully, getCount: ' + albumFetchResult.getCount()); 3643 }).catch((err: BusinessError) => { 3644 console.error('album getPhotoAssets failed with error: ' + err); 3645 }); 3646} 3647``` 3648 3649### commitModify 3650 3651commitModify(callback: AsyncCallback<void>): void 3652 3653Commits the modification on the album attributes to the database. This API uses an asynchronous callback to return the result. 3654 3655**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 3656 3657**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3658 3659**Parameters** 3660 3661| Name | Type | Mandatory| Description | 3662| -------- | ------------------------- | ---- | ---------- | 3663| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 3664 3665**Error codes** 3666 3667For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3668 3669| ID| Error Message| 3670| -------- | ---------------------------------------- | 3671| 401 | if parameter is invalid. | 3672| 13900012 | Permission denied. | 3673| 13900020 | Invalid argument. | 3674| 14000011 | System inner fail. | 3675 3676**Example** 3677 3678```ts 3679import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3680 3681async function example() { 3682 console.info('albumCommitModifyDemo'); 3683 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3684 let albumFetchOptions: photoAccessHelper.FetchOptions = { 3685 fetchColumns: [], 3686 predicates: predicates 3687 }; 3688 let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 3689 let album: photoAccessHelper.Album = await albumList.getFirstObject(); 3690 album.albumName = 'hello'; 3691 album.commitModify((err) => { 3692 if (err !== undefined) { 3693 console.error('commitModify failed with error: ' + err); 3694 } else { 3695 console.info('commitModify successfully'); 3696 } 3697 }); 3698} 3699``` 3700 3701### commitModify 3702 3703commitModify(): Promise<void> 3704 3705Commits the modification on the album attributes to the database. This API uses a promise to return the result. 3706 3707**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 3708 3709**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3710 3711**Return value** 3712 3713| Type | Description | 3714| ------------------- | ------------ | 3715| Promise<void> | Promise that returns no value.| 3716 3717**Error codes** 3718 3719For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3720 3721| ID| Error Message| 3722| -------- | ---------------------------------------- | 3723| 401 | if parameter is invalid. | 3724| 13900012 | Permission denied. | 3725| 13900020 | Invalid argument. | 3726| 14000011 | System inner fail. | 3727 3728**Example** 3729 3730```ts 3731import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3732import { BusinessError } from '@ohos.base'; 3733 3734async function example() { 3735 console.info('albumCommitModifyDemo'); 3736 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3737 let albumFetchOptions: photoAccessHelper.FetchOptions = { 3738 fetchColumns: [], 3739 predicates: predicates 3740 }; 3741 let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 3742 let album: photoAccessHelper.Album = await albumList.getFirstObject(); 3743 album.albumName = 'hello'; 3744 album.commitModify().then(() => { 3745 console.info('commitModify successfully'); 3746 }).catch((err: BusinessError) => { 3747 console.error('commitModify failed with error: ' + err); 3748 }); 3749} 3750``` 3751 3752### addAssets 3753 3754addAssets(assets: Array<PhotoAsset>, callback: AsyncCallback<void>): void 3755 3756Adds image and video assets to an album. Before the operation, ensure that the image and video assets to add and the album exist. This API uses an asynchronous callback to return the result. 3757 3758**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 3759 3760**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3761 3762**Parameters** 3763 3764| Name | Type | Mandatory| Description | 3765| -------- | ------------------------- | ---- | ---------- | 3766| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image and video assets to add.| 3767| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 3768 3769**Error codes** 3770 3771For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3772 3773| ID| Error Message| 3774| -------- | ---------------------------------------- | 3775| 401 | if parameter is invalid. | 3776| 13900012 | Permission denied. | 3777| 13900020 | Invalid argument. | 3778| 14000011 | System inner fail. | 3779 3780**Example** 3781 3782```ts 3783import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3784 3785async function example() { 3786 try { 3787 console.info('addAssetsDemoCallback'); 3788 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3789 let fetchOption: photoAccessHelper.FetchOptions = { 3790 fetchColumns: [], 3791 predicates: predicates 3792 }; 3793 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC); 3794 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 3795 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3796 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 3797 album.addAssets([asset], (err) => { 3798 if (err === undefined) { 3799 console.info('album addAssets successfully'); 3800 } else { 3801 console.error('album addAssets failed with error: ' + err); 3802 } 3803 }); 3804 } catch (err) { 3805 console.error('addAssetsDemoCallback failed with error: ' + err); 3806 } 3807} 3808``` 3809 3810### addAssets 3811 3812addAssets(assets: Array<PhotoAsset>): Promise<void> 3813 3814Adds image and video assets to an album. Before the operation, ensure that the image and video assets to add and the album exist. This API uses a promise to return the result. 3815 3816**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 3817 3818**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3819 3820**Parameters** 3821 3822| Name | Type | Mandatory| Description | 3823| -------- | ------------------------- | ---- | ---------- | 3824| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image and video assets to add.| 3825 3826**Return value** 3827 3828| Type | Description | 3829| --------------------------------------- | ----------------- | 3830|Promise<void> | Promise that returns no value.| 3831 3832**Error codes** 3833 3834For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3835 3836| ID| Error Message| 3837| -------- | ---------------------------------------- | 3838| 401 | if parameter is invalid. | 3839| 13900012 | Permission denied. | 3840| 13900020 | Invalid argument. | 3841| 14000011 | System inner fail. | 3842 3843**Example** 3844 3845```ts 3846import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3847import { BusinessError } from '@ohos.base'; 3848 3849async function example() { 3850 try { 3851 console.info('addAssetsDemoPromise'); 3852 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3853 let fetchOption: photoAccessHelper.FetchOptions = { 3854 fetchColumns: [], 3855 predicates: predicates 3856 }; 3857 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC); 3858 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 3859 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 3860 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 3861 album.addAssets([asset]).then(() => { 3862 console.info('album addAssets successfully'); 3863 }).catch((err: BusinessError) => { 3864 console.error('album addAssets failed with error: ' + err); 3865 }); 3866 } catch (err) { 3867 console.error('addAssetsDemoPromise failed with error: ' + err); 3868 } 3869} 3870``` 3871 3872### removeAssets 3873 3874removeAssets(assets: Array<PhotoAsset>, callback: AsyncCallback<void>): void 3875 3876Removes image and video assets from an album. The album and file resources must exist. This API uses an asynchronous callback to return the result. 3877 3878**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 3879 3880**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3881 3882**Parameters** 3883 3884| Name | Type | Mandatory| Description | 3885| -------- | ------------------------- | ---- | ---------- | 3886| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image and video assets to remove.| 3887| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 3888 3889**Error codes** 3890 3891For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3892 3893| ID| Error Message| 3894| -------- | ---------------------------------------- | 3895| 401 | if parameter is invalid. | 3896| 13900012 | Permission denied. | 3897| 13900020 | Invalid argument. | 3898| 14000011 | System inner fail. | 3899 3900**Example** 3901 3902```ts 3903import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3904 3905async function example() { 3906 try { 3907 console.info('removeAssetsDemoCallback'); 3908 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3909 let fetchOption: photoAccessHelper.FetchOptions = { 3910 fetchColumns: [], 3911 predicates: predicates 3912 }; 3913 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC); 3914 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 3915 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 3916 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 3917 album.removeAssets([asset], (err) => { 3918 if (err === undefined) { 3919 console.info('album removeAssets successfully'); 3920 } else { 3921 console.error('album removeAssets failed with error: ' + err); 3922 } 3923 }); 3924 } catch (err) { 3925 console.error('removeAssetsDemoCallback failed with error: ' + err); 3926 } 3927} 3928``` 3929 3930### removeAssets 3931 3932removeAssets(assets: Array<PhotoAsset>): Promise<void> 3933 3934Removes image and video assets from an album. The album and file resources must exist. This API uses a promise to return the result. 3935 3936**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 3937 3938**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 3939 3940**Parameters** 3941 3942| Name | Type | Mandatory| Description | 3943| -------- | ------------------------- | ---- | ---------- | 3944| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image and video assets to remove.| 3945 3946**Return value** 3947 3948| Type | Description | 3949| --------------------------------------- | ----------------- | 3950|Promise<void> | Promise that returns no value.| 3951 3952**Error codes** 3953 3954For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 3955 3956| ID| Error Message| 3957| -------- | ---------------------------------------- | 3958| 401 | if parameter is invalid. | 3959| 13900012 | Permission denied. | 3960| 13900020 | Invalid argument. | 3961| 14000011 | System inner fail. | 3962 3963**Example** 3964 3965```ts 3966import dataSharePredicates from '@ohos.data.dataSharePredicates'; 3967import { BusinessError } from '@ohos.base'; 3968 3969async function example() { 3970 try { 3971 console.info('removeAssetsDemoPromise'); 3972 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 3973 let fetchOption: photoAccessHelper.FetchOptions = { 3974 fetchColumns: [], 3975 predicates: predicates 3976 }; 3977 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC); 3978 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 3979 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 3980 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 3981 album.removeAssets([asset]).then(() => { 3982 console.info('album removeAssets successfully'); 3983 }).catch((err: BusinessError) => { 3984 console.error('album removeAssets failed with error: ' + err); 3985 }); 3986 } catch (err) { 3987 console.error('removeAssetsDemoPromise failed with error: ' + err); 3988 } 3989} 3990``` 3991 3992### recoverAssets 3993 3994recoverAssets(assets: Array<PhotoAsset>, callback: AsyncCallback<void>): void 3995 3996Recovers image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses an asynchronous callback to return the result. 3997 3998**System API**: This is a system API. 3999 4000**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 4001 4002**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4003 4004**Parameters** 4005 4006| Name | Type | Mandatory| Description | 4007| -------- | ------------------------- | ---- | ---------- | 4008| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image or video assets to recover.| 4009| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 4010 4011**Error codes** 4012 4013For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 4014 4015| ID| Error Message| 4016| -------- | ---------------------------------------- | 4017| 202 | Called by non-system application. | 4018| 401 | if parameter is invalid. | 4019| 13900012 | Permission denied. | 4020| 13900020 | Invalid argument. | 4021| 14000011 | System inner fail. | 4022 4023**Example** 4024 4025```ts 4026import dataSharePredicates from '@ohos.data.dataSharePredicates'; 4027 4028async function example() { 4029 try { 4030 console.info('recoverAssetsDemoCallback'); 4031 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 4032 let fetchOption: photoAccessHelper.FetchOptions = { 4033 fetchColumns: [], 4034 predicates: predicates 4035 }; 4036 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH); 4037 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 4038 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 4039 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 4040 album.recoverAssets([asset], (err) => { 4041 if (err === undefined) { 4042 console.info('album recoverAssets successfully'); 4043 } else { 4044 console.error('album recoverAssets failed with error: ' + err); 4045 } 4046 }); 4047 } catch (err) { 4048 console.error('recoverAssetsDemoCallback failed with error: ' + err); 4049 } 4050} 4051``` 4052 4053### recoverAssets 4054 4055recoverAssets(assets: Array<PhotoAsset>): Promise<void> 4056 4057Recovers image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses a promise to return the result. 4058 4059**System API**: This is a system API. 4060 4061**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 4062 4063**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4064 4065**Parameters** 4066 4067| Name | Type | Mandatory| Description | 4068| -------- | ------------------------- | ---- | ---------- | 4069| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image or video assets to recover.| 4070 4071**Return value** 4072 4073| Type | Description | 4074| --------------------------------------- | ----------------- | 4075|Promise<void> | Promise that returns no value.| 4076 4077**Error codes** 4078 4079For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 4080 4081| ID| Error Message| 4082| -------- | ---------------------------------------- | 4083| 202 | Called by non-system application. | 4084| 401 | if parameter is invalid. | 4085| 13900012 | Permission denied. | 4086| 13900020 | Invalid argument. | 4087| 14000011 | System inner fail. | 4088 4089**Example** 4090 4091```ts 4092import dataSharePredicates from '@ohos.data.dataSharePredicates'; 4093import { BusinessError } from '@ohos.base'; 4094 4095async function example() { 4096 try { 4097 console.info('recoverAssetsDemoPromise'); 4098 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 4099 let fetchOption: photoAccessHelper.FetchOptions = { 4100 fetchColumns: [], 4101 predicates: predicates 4102 }; 4103 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH); 4104 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 4105 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 4106 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 4107 album.recoverAssets([asset]).then(() => { 4108 console.info('album recoverAssets successfully'); 4109 }).catch((err: BusinessError) => { 4110 console.error('album recoverAssets failed with error: ' + err); 4111 }); 4112 } catch (err) { 4113 console.error('recoverAssetsDemoPromise failed with error: ' + err); 4114 } 4115} 4116``` 4117 4118### deleteAssets 4119 4120deleteAssets(assets: Array<PhotoAsset>, callback: AsyncCallback<void>): void 4121 4122Deletes image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses an asynchronous callback to return the result. 4123 4124> **NOTE**<br>This operation is irreversible. The file assets deleted cannot be restored. Exercise caution when performing this operation. 4125 4126**System API**: This is a system API. 4127 4128**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 4129 4130**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4131 4132**Parameters** 4133 4134| Name | Type | Mandatory| Description | 4135| -------- | ------------------------- | ---- | ---------- | 4136| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image or video assets to delete.| 4137| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 4138 4139**Error codes** 4140 4141For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 4142 4143| ID| Error Message| 4144| -------- | ---------------------------------------- | 4145| 202 | Called by non-system application. | 4146| 401 | if parameter is invalid. | 4147| 13900012 | Permission denied. | 4148| 13900020 | Invalid argument. | 4149| 14000011 | System inner fail. | 4150 4151**Example** 4152 4153```ts 4154import dataSharePredicates from '@ohos.data.dataSharePredicates'; 4155 4156async function example() { 4157 try { 4158 console.info('deleteAssetsDemoCallback'); 4159 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 4160 let fetchOption: photoAccessHelper.FetchOptions = { 4161 fetchColumns: [], 4162 predicates: predicates 4163 }; 4164 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH); 4165 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 4166 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 4167 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 4168 album.deleteAssets([asset], (err) => { 4169 if (err === undefined) { 4170 console.info('album deleteAssets successfully'); 4171 } else { 4172 console.error('album deleteAssets failed with error: ' + err); 4173 } 4174 }); 4175 } catch (err) { 4176 console.error('deleteAssetsDemoCallback failed with error: ' + err); 4177 } 4178} 4179``` 4180 4181### deleteAssets 4182 4183deleteAssets(assets: Array<PhotoAsset>): Promise<void> 4184 4185Deletes image or video assets from the trash. Before the operation, ensure that the image or video assets exist in the trash. This API uses a promise to return the result. 4186 4187> **NOTE**<br>This operation is irreversible. The file assets deleted cannot be restored. Exercise caution when performing this operation. 4188 4189**System API**: This is a system API. 4190 4191**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 4192 4193**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4194 4195**Parameters** 4196 4197| Name | Type | Mandatory| Description | 4198| -------- | ------------------------- | ---- | ---------- | 4199| assets | Array<[PhotoAsset](#photoasset)> | Yes | Array of the image or video assets to delete.| 4200 4201**Return value** 4202 4203| Type | Description | 4204| --------------------------------------- | ----------------- | 4205|Promise<void> | Promise that returns no value.| 4206 4207**Error codes** 4208 4209For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 4210 4211| ID| Error Message| 4212| -------- | ---------------------------------------- | 4213| 202 | Called by non-system application. | 4214| 401 | if parameter is invalid. | 4215| 13900012 | Permission denied. | 4216| 13900020 | Invalid argument. | 4217| 14000011 | System inner fail. | 4218 4219**Example** 4220 4221```ts 4222import dataSharePredicates from '@ohos.data.dataSharePredicates'; 4223import { BusinessError } from '@ohos.base'; 4224 4225async function example() { 4226 try { 4227 console.info('deleteAssetsDemoPromise'); 4228 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 4229 let fetchOption: photoAccessHelper.FetchOptions = { 4230 fetchColumns: [], 4231 predicates: predicates 4232 }; 4233 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.TRASH); 4234 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 4235 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 4236 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 4237 album.deleteAssets([asset]).then(() => { 4238 console.info('album deleteAssets successfully'); 4239 }).catch((err: BusinessError) => { 4240 console.error('album deleteAssets failed with error: ' + err); 4241 }); 4242 } catch (err) { 4243 console.error('deleteAssetsDemoPromise failed with error: ' + err); 4244 } 4245} 4246``` 4247 4248### setCoverUri 4249 4250setCoverUri(uri: string, callback: AsyncCallback<void>): void 4251 4252Sets the album cover. This API uses an asynchronous callback to return the result. 4253 4254> **NOTE**<br>This API can be used to set the user album cover, but not the system album cover. 4255 4256**System API**: This is a system API. 4257 4258**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 4259 4260**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4261 4262**Parameters** 4263 4264| Name | Type | Mandatory| Description | 4265| -------- | ------------------------- | ---- | ---------- | 4266| uri | string | Yes | URI of the file to be set as the album cover.| 4267| callback | AsyncCallback<void> | Yes | Callback that returns no value.| 4268 4269**Error codes** 4270 4271For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 4272 4273| ID| Error Message| 4274| -------- | ---------------------------------------- | 4275| 202 | Called by non-system application. | 4276| 401 | if parameter is invalid. | 4277| 13900012 | Permission denied. | 4278| 13900020 | Invalid argument. | 4279| 14000011 | System inner fail. | 4280 4281**Example** 4282 4283```ts 4284import dataSharePredicates from '@ohos.data.dataSharePredicates'; 4285 4286async function example() { 4287 try { 4288 console.info('setCoverUriDemoCallback'); 4289 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 4290 let fetchOption: photoAccessHelper.FetchOptions = { 4291 fetchColumns: [], 4292 predicates: predicates 4293 }; 4294 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC); 4295 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 4296 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 4297 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 4298 album.setCoverUri(asset.uri, (err) => { 4299 if (err === undefined) { 4300 console.info('album setCoverUri successfully'); 4301 } else { 4302 console.error('album setCoverUri failed with error: ' + err); 4303 } 4304 }); 4305 } catch (err) { 4306 console.error('setCoverUriDemoCallback failed with error: ' + err); 4307 } 4308} 4309``` 4310 4311### setCoverUri 4312 4313setCoverUri(uri: string): Promise<void> 4314 4315Sets the album cover. This API uses a promise to return the result. 4316 4317> **NOTE**<br>This API can be used to set the user album cover, but not the system album cover. 4318 4319**System API**: This is a system API. 4320 4321**Required permissions**: ohos.permission.WRITE_IMAGEVIDEO 4322 4323**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4324 4325**Parameters** 4326 4327| Name | Type | Mandatory| Description | 4328| -------- | ------------------------- | ---- | ---------- | 4329| uri | string | Yes | URI of the file to be set as the album cover.| 4330 4331**Return value** 4332 4333| Type | Description | 4334| --------------------------------------- | ----------------- | 4335|Promise<void> | Promise that returns no value.| 4336 4337**Error codes** 4338 4339For details about the error codes, see [Universal Error Codes](../errorcodes/errorcode-universal.md) and [File Management Error Codes](../errorcodes/errorcode-filemanagement.md). 4340 4341| ID| Error Message| 4342| -------- | ---------------------------------------- | 4343| 202 | Called by non-system application. | 4344| 401 | if parameter is invalid. | 4345| 13900012 | Permission denied. | 4346| 13900020 | Invalid argument. | 4347| 14000011 | System inner fail. | 4348**Example** 4349 4350```ts 4351import dataSharePredicates from '@ohos.data.dataSharePredicates'; 4352import { BusinessError } from '@ohos.base'; 4353 4354async function example() { 4355 try { 4356 console.info('setCoverUriDemoPromise'); 4357 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 4358 let fetchOption: photoAccessHelper.FetchOptions = { 4359 fetchColumns: [], 4360 predicates: predicates 4361 }; 4362 let albumFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC); 4363 let album: photoAccessHelper.Album = await albumFetchResult.getFirstObject(); 4364 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await album.getAssets(fetchOption); 4365 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 4366 album.setCoverUri(asset.uri).then(() => { 4367 console.info('album setCoverUri successfully'); 4368 }).catch((err: BusinessError) => { 4369 console.error('album setCoverUri failed with error: ' + err); 4370 }); 4371 } catch (err) { 4372 console.error('setCoverUriDemoPromise failed with error: ' + err); 4373 } 4374} 4375``` 4376 4377## MemberType 4378 4379Enumerates the member types. 4380 4381**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4382 4383| Name | Type| Readable | Writable | Description | 4384| ----- | ---- | ---- | ---- | ---- | 4385| number | number | Yes| Yes| The member is a number.| 4386| string | string | Yes| Yes| The member is a string.| 4387| boolean | boolean | Yes| Yes| The member is a Boolean value.| 4388 4389## PhotoType 4390 4391Enumerates media file types. 4392 4393**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4394 4395| Name | Value| Description| 4396| ----- | ---- | ---- | 4397| IMAGE | 1 | Image.| 4398| VIDEO | 2 | Video.| 4399 4400## PhotoSubtype 4401 4402Enumerates the [PhotoAsset](#photoasset) types. 4403 4404**System API**: This is a system API. 4405 4406**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4407 4408| Name | Value| Description| 4409| ----- | ---- | ---- | 4410| DEFAULT | 0 | Default (photo) type.| 4411| SCREENSHOT | 1 | Screenshot and screen recording file.| 4412 4413## PositionType 4414 4415Enumerates the file locations. 4416 4417**System API**: This is a system API. 4418 4419**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4420 4421| Name | Value| Description| 4422| ----- | ---- | ---- | 4423| LOCAL | 1 << 0 | Stored only on a local device.| 4424| CLOUD | 1 << 1 | Stored only on the cloud.| 4425 4426## AlbumType 4427 4428Enumerates the album types. 4429 4430**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4431 4432| Name | Value| Description| 4433| ----- | ---- | ---- | 4434| USER | 0 | User album.| 4435| SYSTEM | 1024 | System album.| 4436 4437## AlbumSubtype 4438 4439Enumerate the album subtypes. 4440 4441**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4442 4443| Name | Value| Description| 4444| ----- | ---- | ---- | 4445| USER_GENERIC | 1 | User album.| 4446| FAVORITE | 1025 | Favorites.| 4447| VIDEO | 1026 | Video album.| 4448| HIDDEN | 1027 | Hidden album.<br>**System API**: This is a system API.| 4449| TRASH | 1028 | Trash.<br>**System API**: This is a system API.| 4450| SCREENSHOT | 1029 | Album for screenshots and screen recording files.<br>**System API**: This is a system API.| 4451| CAMERA | 1030 | Album for photos and videos taken by the camera.<br>**System API**: This is a system API.| 4452| ANY | 2147483647 | Any album.| 4453 4454## PhotoKeys 4455 4456Defines the key information about an image or video file. 4457 4458**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4459 4460| Name | Value | Description | 4461| ------------- | ------------------- | ---------------------------------------------------------- | 4462| URI | 'uri' | URI of the file. | 4463| PHOTO_TYPE | 'media_type' | Type of the file. | 4464| DISPLAY_NAME | 'display_name' | File name displayed. | 4465| SIZE | 'size' | File size. | 4466| DATE_ADDED | 'date_added' | Date when the file was added. The value is the number of seconds elapsed since the Epoch time (00:00:00 UTC on January 1, 1970). | 4467| 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.| 4468| DURATION | 'duration' | Duration, in ms. | 4469| WIDTH | 'width' | Image width, in pixels. | 4470| HEIGHT | 'height' | Image height, in pixels. | 4471| DATE_TAKEN | 'date_taken' | Date when the file (photo) was taken. The value is the number of seconds elapsed since the Epoch time. | 4472| ORIENTATION | 'orientation' | Orientation of the image file. | 4473| FAVORITE | 'is_favorite' | Whether the file is added to favorites. | 4474| TITLE | 'title' | Title in the file. | 4475| POSITION | 'position' | File location type.<br>**System API**: This is a system API. | 4476| DATE_TRASHED | 'date_trashed' | Date when the file was deleted. The value is the number of seconds elapsed since the Epoch time.<br>**System API**: This is a system API. | 4477| HIDDEN | 'hidden' | Whether the file is hidden.<br>**System API**: This is a system API. | 4478| CAMERA_SHOT_KEY | 'camera_shot_key' | Key for the Ultra Snapshot feature, which allows the camera to take photos or record videos with the screen off. (This parameter is available only for the system camera, and the key value is defined by the system camera.)<br>**System API**: This is a system API. | 4479| USER_COMMENT<sup>10+</sup> | 'user_comment' | User comment information.<br>**System API**: This is a system API. | 4480 4481## AlbumKeys 4482 4483Enumerates the key album attributes. 4484 4485**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4486 4487| Name | Value | Description | 4488| ------------- | ------------------- | ---------------------------------------------------------- | 4489| URI | 'uri' | URI of the album. | 4490| ALBUM_NAME | 'album_name' | Name of the album. | 4491 4492## PhotoCreateOptions 4493 4494Defines the options for creating an image or video asset. 4495 4496**System API**: This is a system API. 4497 4498**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4499 4500| Name | Type | Mandatory| Description | 4501| ---------------------- | ------------------- | ---- | ------------------------------------------------ | 4502| subtype | [PhotoSubtype](#photosubtype) | No | Subtype of the image or video. | 4503| cameraShotKey | string | No | Key for the Ultra Snapshot feature, which allows the camera to take photos or record videos with the screen off. (This parameter is available only for the system camera, and the key value is defined by the system camera.) | 4504 4505## CreateOptions 4506 4507Defines the options for creating an image or video asset. 4508 4509**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4510 4511| Name | Type | Mandatory| Description | 4512| ---------------------- | ------------------- | ---- | ------------------------------------------------ | 4513| title | string | No | Title of the image or video. | 4514 4515## FetchOptions 4516 4517Defines the options for fetching media files. 4518 4519**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4520 4521| Name | Type | Readable| Writable| Description | 4522| ---------------------- | ------------------- | ---- |---- | ------------------------------------------------ | 4523| fetchColumns | Array<string> | Yes | Yes | Options for fetching files based on the attributes in columns.<br>If this parameter is left empty, files are fetched by URI, name, and type (the specific field names vary with the file asset or album object) by default. In addition, an error will be reported if [get](#get) is called to obtain other attributes of this object. <br>Example: fetchColumns: ['uri', 'title']| 4524| predicates | [dataSharePredicates.DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes | Yes | Predicates that specify the fetch criteria.| 4525 4526## ChangeData 4527 4528Defines the return value of the listener callback. 4529 4530**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4531 4532| Name | Type | Readable| Writable| Description | 4533| ------- | --------------------------- | ---- | ---- | ------------------------------------------------------------ | 4534| type | [NotifyType](#notifytype) | Yes | No | Notification type. | 4535| uris | Array<string> | Yes | No | All URIs with the same [NotifyType](#notifytype), which can be **PhotoAsset** or **Album**.| 4536| extraUris | Array<string> | Yes | No | URIs of the changed files in the album. | 4537 4538## NotifyType 4539 4540Enumerates the notification event types. 4541 4542**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4543 4544| Name | Value | Description | 4545| ------------------------- | ---- | -------------------------------- | 4546| NOTIFY_ADD | 0 | A file asset or album is added. | 4547| NOTIFY_UPDATE | 1 | A file asset or album is updated. | 4548| NOTIFY_REMOVE | 2 | A file asset or album is removed. | 4549| NOTIFY_ALBUM_ADD_ASSET | 3 | A file asset is added to the album.| 4550| NOTIFY_ALBUM_REMOVE_ASSET | 4 | A file asset is removed from the album.| 4551 4552## DefaultChangeUri 4553 4554Enumerates the **DefaultChangeUri** subtypes. 4555 4556**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4557 4558| Name | Value | Description | 4559| ----------------- | ----------------------- | ------------------------------------------------------------ | 4560| DEFAULT_PHOTO_URI | 'file://media/Photo' | Default **PhotoAsset** URI, which must be used with **forChildUris{true}** to subscribe to change notifications of all photo assets.| 4561| DEFAULT_ALBUM_URI | 'file://media/PhotoAlbum' | Default album URI, which must be used with **forChildUris{true}** to subscribe to change notifications of all albums.| 4562 4563## PhotoViewMIMETypes 4564 4565Enumerates the media file types that can be selected. 4566 4567**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4568 4569| Name | Value| Description| 4570| ----- | ---- | ---- | 4571| IMAGE_TYPE | 'image/*' | Image.| 4572| VIDEO_TYPE | 'video/*' | Video.| 4573| IMAGE_VIDEO_TYPE | '\*/*' | Image and video.| 4574 4575## PhotoSelectOptions 4576 4577Defines the options for selecting images or videos. 4578 4579**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4580 4581| Name | Type | Mandatory| Description | 4582| ----------------------- | ------------------- | ---- | -------------------------------- | 4583| MIMEType | [PhotoViewMIMETypes](#photoviewmimetypes) | No | Available media file types. **IMAGE_VIDEO_TYPE** is used by default.| 4584| maxSelectNumber | number | No | Maximum number of media files that can be selected. The default value is **50**, and the maximum value is **500**. | 4585 4586## PhotoSelectResult 4587 4588Defines information about the images or videos selected. 4589 4590**System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core 4591 4592| Name | Type | Readable| Writable| Description | 4593| ----------------------- | ------------------- | ---- | ---- | ------------------------------ | 4594| photoUris | Array<string> | Yes | Yes | URIs of the images or videos selected. The URI array can be used only by calling [photoAccessHelper.getAssets](#getassets) with temporary authorization. For details about how to use the media file URI, see [Using a Media File URI] (../../file-management/user-file-uri-intro.md#using-a-media-file-uri).| 4595| isOriginalPhoto | boolean | Yes | Yes | Whether the selected media asset is the original image.| 4596