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