1# @ohos.file.sendablePhotoAccessHelper (基于Sendable对象的相册管理模块) 2 3该模块基于Sendable对象,提供相册管理模块能力,包括创建相册以及访问、修改相册中的媒体数据信息等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9## 导入模块 10 11```ts 12import { sendablePhotoAccessHelper } from '@kit.MediaLibraryKit'; 13``` 14 15## sendablePhotoAccessHelper.getPhotoAccessHelper 16 17getPhotoAccessHelper(context: Context): PhotoAccessHelper 18 19获取相册管理模块的实例,用于访问和修改相册中的媒体文件。 20 21**模型约束**: 此接口仅可在Stage模型下使用。 22 23**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 24 25**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 26 27**参数:** 28 29| 参数名 | 类型 | 必填 | 说明 | 30| ------- | ------------------------------------------------------------ | ---- | -------------------------- | 31| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | 是 | 传入Ability实例的Context。 | 32 33**返回值:** 34 35| 类型 | 说明 | 36| --------------------------------------- | :------------------- | 37| [PhotoAccessHelper](#photoaccesshelper) | 相册管理模块的实例。 | 38 39**错误码:** 40 41接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 42 43| 错误码ID | 错误信息 | 44| -------- | ------------------------------------------------------------ | 45| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 46 47**示例:** 48 49```ts 50//此处获取的phAccessHelper实例为全局对象,后续使用到phAccessHelper的地方默认为使用此处获取的对象,如未添加此段代码报phAccessHelper未定义的错误请自行添加 51let context = getContext(this); 52let phAccessHelper = sendablePhotoAccessHelper.getPhotoAccessHelper(context); 53``` 54 55## PhotoAccessHelper 56 57### getAssets 58 59getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 60 61获取图片和视频资源,使用Promise方式返回结果。 62 63**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 64 65**需要权限**:ohos.permission.READ_IMAGEVIDEO 66 67对于未申请'ohos.permission.READ_IMAGEVIDEO'权限的应用,可以通过picker的方式调用该接口来查询指定uri对应的图片或视频资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-photoviewpicker.md#指定uri获取图片或视频资源)。 68 69**参数:** 70 71| 参数名 | 类型 | 必填 | 说明 | 72| ------- | --------------------------------------------------------- | ---- | -------------------- | 73| options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 图片和视频检索选项。 | 74 75**返回值:** 76 77| 类型 | 说明 | 78| ------------------------------------------------------------ | --------------------------------------- | 79| Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise对象,返回图片和视频数据结果集。 | 80 81**错误码:** 82 83接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 84 85| 错误码ID | 错误信息 | 86| -------- | ------------------------------------------------------------ | 87| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 88| 201 | Permission denied. | 89| 13900020 | Invalid argument. | 90| 14000011 | Internal system error | 91 92**示例:** 93 94 95 96```ts 97import { dataSharePredicates } from '@kit.ArkData'; 98import { photoAccessHelper } from '@kit.MediaLibraryKit'; 99 100async function example() { 101 console.info('getAssets'); 102 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 103 let fetchOptions: photoAccessHelper.FetchOptions = { 104 fetchColumns: [], 105 predicates: predicates 106 }; 107 try { 108 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 109 if (fetchResult !== undefined) { 110 console.info('fetchResult success'); 111 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 112 if (photoAsset !== undefined) { 113 console.info('photoAsset.displayName :' + photoAsset.displayName); 114 } 115 } 116 } catch (err) { 117 console.error(`getAssets failed, error: ${err.code}, ${err.message}`); 118 } 119} 120``` 121 122### getBurstAssets 123 124getBurstAssets(burstKey: string, options: FetchOptions): Promise<FetchResult<PhotoAsset>> 125 126获取连拍照片资源,使用Promise方式返回结果。 127 128**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 129 130**需要权限**:ohos.permission.READ_IMAGEVIDEO 131 132**参数:** 133 134| 参数名 | 类型 | 必填 | 说明 | 135| -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | 136| burstKey | string | 是 | 一组连拍照片的唯一标识:uuid(可传入[PhotoKeys](js-apis-photoAccessHelper.md#photokeys)的BURST_KEY) | 137| options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 连拍照片检索选项。 | 138 139**返回值:** 140 141| 类型 | 说明 | 142| ------------------------------------------------------------ | ------------------------------------- | 143| Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise对象,返回连拍照片数据结果集。 | 144 145**错误码:** 146 147接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 148 149| 错误码ID | 错误信息 | 150| -------- | ------------------------------------------------------------ | 151| 201 | Permission denied. | 152| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 153| 14000011 | Internal system error. | 154 155**示例:** 156 157```ts 158import { photoAccessHelper } from '@kit.MediaLibraryKit'; 159import { dataSharePredicates } from '@kit.ArkData'; 160 161async function example() { 162 console.info('getBurstAssets'); 163 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 164 let fetchOption: photoAccessHelper.FetchOptions = { 165 fetchColumns: [], 166 predicates: predicates 167 }; 168 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 169 let photoAssetList: Array<sendablePhotoAccessHelper.PhotoAsset> = await fetchResult.getAllObjects(); 170 let photoAsset: sendablePhotoAccessHelper.PhotoAsset; 171 // burstKey为36位的uuid,可以根据photoAccessHelper.PhotoKeys获取。 172 for(photoAsset of photoAssetList){ 173 let burstKey: string = photoAccessHelper.PhotoKeys.BURST_KEY.toString(); 174 let photoAccessBurstKey: photoAccessHelper.MemberType = photoAsset.get(burstKey).toString(); 175 try { 176 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await 177 phAccessHelper.getBurstAssets(photoAccessBurstKey, fetchOption); 178 if (fetchResult !== undefined) { 179 console.info('fetchResult success'); 180 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 181 if (photoAsset !== undefined) { 182 console.info('photoAsset.displayName :' + photoAsset.displayName); 183 } 184 } 185 } catch (err) { 186 console.error(`getBurstAssets failed, error: ${err.code}, ${err.message}`); 187 } 188} 189} 190``` 191 192### createAsset 193 194createAsset(photoType: PhotoType, extension: string, options?: CreateOptions): Promise<string> 195 196指定待创建的文件类型、后缀和创建选项,创建图片或视频资源,使用Promise方式返回结果。 197 198此接口在未申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'时,可以使用安全控件创建媒体资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-savebutton.md)。 199 200**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 201 202**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 203 204**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 205 206**参数:** 207 208| 参数名 | 类型 | 必填 | 说明 | 209| --------- | ----------------------------------------------------------- | ---- | ------------------------------------ | 210| photoType | [PhotoType](#phototype) | 是 | 创建的文件类型,IMAGE或者VIDEO类型。 | 211| extension | string | 是 | 文件名后缀参数,例如:'jpg'。 | 212| options | [CreateOptions](js-apis-photoAccessHelper.md#createoptions) | 否 | 创建选项,例如{title: 'testPhoto'}。 | 213 214**返回值:** 215 216| 类型 | 说明 | 217| --------------------- | ---------------------------------------- | 218| Promise<string> | Promise对象,返回创建的图片和视频的uri。 | 219 220**错误码:** 221 222接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 223 224| 错误码ID | 错误信息 | 225| -------- | ------------------------------------------------------------ | 226| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 227| 201 | Permission denied. | 228| 13900020 | Invalid argument. | 229| 14000011 | Internal system error | 230 231**示例:** 232 233```ts 234import { photoAccessHelper } from '@kit.MediaLibraryKit'; 235 236async function example() { 237 console.info('createAssetDemo'); 238 try { 239 let photoType: sendablePhotoAccessHelper.PhotoType = sendablePhotoAccessHelper.PhotoType.IMAGE; 240 let extension: string = 'jpg'; 241 let options: photoAccessHelper.CreateOptions = { 242 title: 'testPhoto' 243 } 244 let uri: string = await phAccessHelper.createAsset(photoType, extension, options); 245 console.info('createAsset uri' + uri); 246 console.info('createAsset successfully'); 247 } catch (err) { 248 console.error(`createAsset failed, error: ${err.code}, ${err.message}`); 249 } 250} 251``` 252 253### getAlbums 254 255getAlbums(type: AlbumType, subtype: AlbumSubtype, options?: FetchOptions): Promise<FetchResult<Album>> 256 257根据检索选项和相册类型获取相册,使用Promise方式返回结果。 258 259获取相册前需先保证相册存在。 260 261**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 262 263**需要权限**:ohos.permission.READ_IMAGEVIDEO 264 265**参数:** 266 267| 参数名 | 类型 | 必填 | 说明 | 268| ------- | --------------------------------------------------------- | ---- | -------------------------------------- | 269| type | [AlbumType](#albumtype) | 是 | 相册类型。 | 270| subtype | [AlbumSubtype](#albumsubtype) | 是 | 相册子类型。 | 271| options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 否 | 检索选项,不填时默认根据相册类型检索。 | 272 273**返回值:** 274 275| 类型 | 说明 | 276| ------------------------------------------------------------ | ----------------------------------- | 277| Promise<[FetchResult](#fetchresult)<[Album](#album)>> | Promise对象,返回获取相册的结果集。 | 278 279**错误码:** 280 281接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 282 283| 错误码ID | 错误信息 | 284| -------- | ------------------------------------------------------------ | 285| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 286| 201 | Permission denied. | 287| 13900020 | Invalid argument. | 288| 14000011 | Internal system error | 289 290**示例:** 291 292```ts 293import { dataSharePredicates } from '@kit.ArkData'; 294import { BusinessError } from '@kit.BasicServicesKit'; 295import { photoAccessHelper } from '@kit.MediaLibraryKit'; 296 297async function example() { 298 // 示例代码中为获取相册名为newAlbumName的相册。 299 console.info('getAlbumsDemo'); 300 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 301 predicates.equalTo('album_name', 'newAlbumName'); 302 let fetchOptions: photoAccessHelper.FetchOptions = { 303 fetchColumns: [], 304 predicates: predicates 305 }; 306 phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions).then( async (fetchResult) => { 307 if (fetchResult === undefined) { 308 console.error('getAlbumsPromise fetchResult is undefined'); 309 return; 310 } 311 let album: sendablePhotoAccessHelper.Album = await fetchResult.getFirstObject(); 312 console.info('getAlbumsPromise successfully, albumName: ' + album.albumName); 313 fetchResult.close(); 314 }).catch((err: BusinessError) => { 315 console.error(`getAlbumsPromise failed with err: ${err.code}, ${err.message}`); 316 }); 317} 318``` 319 320### getAlbums 321 322getAlbums(options: FetchOptions): Promise<FetchResult<Album>> 323 324根据检索选项获取相册,使用Promise方式返回结果。 325 326获取相册前需先保证相册存在。 327 328**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 329 330**需要权限**:ohos.permission.READ_IMAGEVIDEO 331 332**参数:** 333 334| 参数名 | 类型 | 必填 | 说明 | 335| ------- | --------------------------------------------------------- | ---- | -------- | 336| options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 检索选项 | 337 338**返回值:** 339 340| 类型 | 说明 | 341| ------------------------------------------------------------ | ----------------------------------- | 342| Promise<[FetchResult](#fetchresult)<[Album](#album)>> | Promise对象,返回获取相册的结果集。 | 343 344**错误码:** 345 346接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 347 348| 错误码ID | 错误信息 | 349| -------- | ------------------------------------------------------------ | 350| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 351| 201 | Permission denied. | 352| 13900020 | Invalid argument. | 353| 14000011 | Internal system error | 354 355**示例:** 356 357```ts 358import { dataSharePredicates } from '@kit.ArkData'; 359import { BusinessError } from '@kit.BasicServicesKit'; 360import { photoAccessHelper } from '@kit.MediaLibraryKit'; 361 362async function example() { 363 // 示例代码中为获取相册名为newAlbumName的相册。 364 console.info('getAlbumsDemo'); 365 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 366 predicates.equalTo('album_name', 'newAlbumName'); 367 let fetchOptions: photoAccessHelper.FetchOptions = { 368 fetchColumns: [], 369 predicates: predicates 370 }; 371 phAccessHelper.getAlbums(fetchOptions).then( async (fetchResult) => { 372 if (fetchResult === undefined) { 373 console.error('getAlbumsPromise fetchResult is undefined'); 374 return; 375 } 376 let album: sendablePhotoAccessHelper.Album = await fetchResult.getFirstObject(); 377 console.info('getAlbumsPromise successfully, albumName: ' + album.albumName); 378 fetchResult.close(); 379 }).catch((err: BusinessError) => { 380 console.error(`getAlbumsPromise failed with err: ${err.code}, ${err.message}`); 381 }); 382} 383``` 384 385### release 386 387release(): Promise<void> 388 389释放PhotoAccessHelper实例,使用Promise方式返回结果。 390当后续不需要使用PhotoAccessHelper 实例中的方法时调用。 391 392**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 393 394**返回值:** 395 396| 类型 | 说明 | 397| ------------------- | ----------------------- | 398| Promise<void> | Promise对象,返回void。 | 399 400**错误码:** 401 402接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 403 404| 错误码ID | 错误信息 | 405| -------- | ------------------------------------------------------------ | 406| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 407| 13900020 | Invalid argument. | 408| 14000011 | Internal system error | 409 410**示例:** 411 412```ts 413async function example() { 414 console.info('releaseDemo'); 415 try { 416 console.info('use function...'); 417 } catch (err) { 418 console.error(`function error ...`); 419 }finally{ 420 try{ 421 phAccessHelper?.release(); 422 console.info(`release success`); 423 } catch(e){ 424 console.error(`release error :${e}`); 425 } 426 } 427} 428``` 429 430## PhotoAsset 431 432提供封装文件属性的方法。 433 434### 属性 435 436**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 437 438| 名称 | 类型 | 只读 | 可选 | 说明 | 439| ----------- | ----------------------- | ---- | ---- | ------------------------------------------------------------ | 440| uri | string | 是 | 否 | 媒体文件资源uri(如:file://media/Photo/1/IMG_datetime_0001/displayName.jpg),详情参见用户文件uri介绍中的[媒体文件uri](../../file-management/user-file-uri-intro.md#媒体文件uri)。 | 441| photoType | [PhotoType](#phototype) | 是 | 否 | 媒体文件类型 | 442| displayName | string | 是 | 否 | 显示文件名,包含后缀名。 | 443 444### convertToPhotoAsset 445 446convertToPhotoAsset(): photoAccessHelper.PhotoAsset 447 448将Sendable类型PhotoAsset转换为非Sendable类型PhotoAsset。 449 450**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 451 452**返回值:** 453 454| 类型 | 说明 | 455| ---------------------------- | ------------------------------------------------------------ | 456| photoAccessHelper.PhotoAsset | 返回非Sendable类型的[PhotoAsset](js-apis-photoAccessHelper.md#photoasset) | 457 458**错误码:** 459 460接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 461 462| 错误码ID | 错误信息 | 463| -------- | ------------------------------------------------------------ | 464| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 465| 13900020 | Invalid argument. | 466| 14000014 | Member is not a valid PhotoKey. | 467 468**示例:** 469 470```ts 471import { dataSharePredicates } from '@kit.ArkData'; 472import { photoAccessHelper } from '@kit.MediaLibraryKit'; 473 474async function example() { 475 console.info('convertToPhotoAssetDemo'); 476 try { 477 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 478 let fetchOption: photoAccessHelper.FetchOptions = { 479 fetchColumns: ['title'], 480 predicates: predicates 481 }; 482 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 483 let sendablePhotoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 484 let photoAsset: photoAccessHelper.PhotoAsset = sendablePhotoAsset.convertToPhotoAsset(); 485 console.log(`get no sendable uri success : ${photoAsset.uri}`); 486 } catch (err) { 487 console.error(`convertToPhotoAsset failed. error: ${err.code}, ${err.message}`); 488 } 489} 490``` 491 492### get 493 494get(member: string): MemberType 495 496获取PhotoAsset成员参数。 497 498**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 499 500**参数:** 501 502| 参数名 | 类型 | 必填 | 说明 | 503| ------ | ------ | ---- | ------------------------------------------------------------ | 504| member | string | 是 | 成员参数名称,在get时,除了'uri'、'media_type'、'subtype'和'display_name'四个属性之外,其他的属性都需要在fetchColumns中填入需要get的[PhotoKeys](js-apis-photoAccessHelper.md#photokeys),例如:get title属性fetchColumns: ['title']。 | 505 506**返回值:** 507 508| 类型 | 说明 | 509| ----------------------------------------------------- | ---------------------------- | 510| [MemberType](js-apis-photoAccessHelper.md#membertype) | 获取PhotoAsset成员参数的值。 | 511 512**错误码:** 513 514接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 515 516| 错误码ID | 错误信息 | 517| -------- | ------------------------------------------------------------ | 518| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 519| 13900020 | Invalid argument. | 520| 14000014 | Member is not a valid PhotoKey. | 521 522**示例:** 523 524```ts 525import { dataSharePredicates } from '@kit.ArkData'; 526import { photoAccessHelper } from '@kit.MediaLibraryKit'; 527 528async function example() { 529 console.info('photoAssetGetDemo'); 530 try { 531 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 532 let fetchOption: photoAccessHelper.FetchOptions = { 533 fetchColumns: ['title'], 534 predicates: predicates 535 }; 536 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 537 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 538 let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE; 539 let photoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title.toString()); 540 console.info('photoAsset Get photoAssetTitle = ', photoAssetTitle); 541 } catch (err) { 542 console.error(`get failed. error: ${err.code}, ${err.message}`); 543 } 544} 545``` 546 547### set 548 549set(member: string, value: string): void 550 551设置PhotoAsset成员参数。 552 553**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 554 555**参数:** 556 557| 参数名 | 类型 | 必填 | 说明 | 558| ------ | ------ | ---- | ------------------------------------------------------------ | 559| member | string | 是 | 成员参数名称例如:[PhotoKeys](js-apis-photoAccessHelper.md#photokeys).TITLE。 | 560| value | string | 是 | 设置成员参数名称,只能修改[PhotoKeys](js-apis-photoAccessHelper.md#photokeys).TITLE的值。 | 561 562**错误码:** 563 564接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 565 566| 错误码ID | 错误信息 | 567| -------- | ------------------------------------------------------------ | 568| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 569| 13900020 | Invalid argument. | 570| 14000014 | Member is not a valid PhotoKey. | 571 572**示例:** 573 574```ts 575import { dataSharePredicates } from '@kit.ArkData'; 576import { photoAccessHelper } from '@kit.MediaLibraryKit'; 577 578async function example() { 579 console.info('photoAssetSetDemo'); 580 try { 581 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 582 let fetchOption: photoAccessHelper.FetchOptions = { 583 fetchColumns: ['title'], 584 predicates: predicates 585 }; 586 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 587 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 588 let title: string = photoAccessHelper.PhotoKeys.TITLE.toString(); 589 photoAsset.set(title, 'newTitle'); 590 } catch (err) { 591 console.error(`set failed. error: ${err.code}, ${err.message}`); 592 } 593} 594``` 595 596### commitModify 597 598commitModify(): Promise<void> 599 600修改文件的元数据,使用Promise方式返回异步结果。 601 602**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 603 604**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 605 606**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 607 608**返回值:** 609 610| 类型 | 说明 | 611| ------------------- | ----------------------- | 612| Promise<void> | Promise对象,返回void。 | 613 614**错误码:** 615 616接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 617 618| 错误码ID | 错误信息 | 619| -------- | ------------------------------------------------------------ | 620| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 621| 201 | Permission denied. | 622| 13900020 | Invalid argument. | 623| 14000001 | Invalid display name. | 624| 14000011 | Internal system error | 625 626**示例:** 627 628```ts 629import { dataSharePredicates } from '@kit.ArkData'; 630import { photoAccessHelper } from '@kit.MediaLibraryKit'; 631 632async function example() { 633 console.info('commitModifyDemo'); 634 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 635 let fetchOption: photoAccessHelper.FetchOptions = { 636 fetchColumns: ['title'], 637 predicates: predicates 638 }; 639 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 640 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 641 let title: string = photoAccessHelper.PhotoKeys.TITLE.toString(); 642 let photoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); 643 console.info('photoAsset get photoAssetTitle = ', photoAssetTitle); 644 photoAsset.set(title, 'newTitle3'); 645 try { 646 await photoAsset.commitModify(); 647 let newPhotoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); 648 console.info('photoAsset get newPhotoAssetTitle = ', newPhotoAssetTitle); 649 } catch (err) { 650 console.error(`commitModify failed. error: ${err.code}, ${err.message}`); 651 } 652} 653``` 654 655### getThumbnail 656 657getThumbnail(size?: image.Size): Promise<image.PixelMap> 658 659获取文件的缩略图,传入缩略图尺寸,使用Promise方式返回异步结果。 660 661**需要权限**:ohos.permission.READ_IMAGEVIDEO 662 663**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 664 665**参数:** 666 667| 参数名 | 类型 | 必填 | 说明 | 668| ------ | ----------------------------------------------------- | ---- | ------------ | 669| size | [image.Size](../apis-image-kit/js-apis-image.md#size) | 否 | 缩略图尺寸。 | 670 671**返回值:** 672 673| 类型 | 说明 | 674| ------------------------------------------------------------ | ----------------------------------- | 675| Promise<[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)> | Promise对象,返回缩略图的PixelMap。 | 676 677**错误码:** 678 679接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 680 681| 错误码ID | 错误信息 | 682| -------- | ------------------------------------------------------------ | 683| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 684| 201 | Permission denied. | 685| 13900020 | Invalid argument. | 686| 14000011 | Internal system error | 687 688**示例:** 689 690```ts 691import { dataSharePredicates } from '@kit.ArkData'; 692import { image } from '@kit.ImageKit'; 693import { BusinessError } from '@kit.BasicServicesKit'; 694import { photoAccessHelper } from '@kit.MediaLibraryKit'; 695 696async function example() { 697 console.info('getThumbnailDemo'); 698 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 699 let fetchOption: photoAccessHelper.FetchOptions = { 700 fetchColumns: [], 701 predicates: predicates 702 }; 703 let size: image.Size = { width: 720, height: 720 }; 704 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 705 let asset = await fetchResult.getFirstObject(); 706 console.info('asset displayName = ', asset.displayName); 707 asset.getThumbnail(size).then((pixelMap) => { 708 console.info('getThumbnail successful ' + pixelMap); 709 }).catch((err: BusinessError) => { 710 console.error(`getThumbnail fail with error: ${err.code}, ${err.message}`); 711 }); 712} 713``` 714 715## FetchResult 716 717文件检索结果集。 718 719### getCount 720 721getCount(): number 722 723获取文件检索结果中的文件总数。 724 725**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 726 727**返回值:** 728 729| 类型 | 说明 | 730| ------ | ------------------ | 731| number | 检索到的文件总数。 | 732 733**错误码:** 734 735接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 736 737| 错误码ID | 错误信息 | 738| -------- | ------------------------------------------------------------ | 739| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 740| 13900020 | Invalid argument. | 741| 14000011 | Internal system error | 742 743**示例:** 744 745```ts 746import { dataSharePredicates } from '@kit.ArkData'; 747import { photoAccessHelper } from '@kit.MediaLibraryKit'; 748 749async function example() { 750 console.info('getCountDemo'); 751 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 752 let fetchOption: photoAccessHelper.FetchOptions = { 753 fetchColumns: [], 754 predicates: predicates 755 }; 756 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 757 let fetchCount = fetchResult.getCount(); 758 console.info('fetchCount = ', fetchCount); 759} 760``` 761 762### isAfterLast 763 764isAfterLast(): boolean 765 766检查结果集是否指向最后一行。 767 768**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 769 770**返回值:** 771 772| 类型 | 说明 | 773| ------- | ----------------------------------------------------------- | 774| boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 | 775 776**错误码:** 777 778接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 779 780| 错误码ID | 错误信息 | 781| -------- | ------------------------------------------------------------ | 782| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 783| 13900020 | Invalid argument. | 784| 14000011 | Internal system error | 785 786**示例:** 787 788```ts 789import { dataSharePredicates } from '@kit.ArkData'; 790import { photoAccessHelper } from '@kit.MediaLibraryKit'; 791 792async function example() { 793 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 794 let fetchOption: photoAccessHelper.FetchOptions = { 795 fetchColumns: [], 796 predicates: predicates 797 }; 798 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 799 let fetchCount = fetchResult.getCount(); 800 console.info('count:' + fetchCount); 801 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); 802 if (fetchResult.isAfterLast()) { 803 console.info('photoAsset isAfterLast displayName = ', photoAsset.displayName); 804 } else { 805 console.info('photoAsset not isAfterLast.'); 806 } 807} 808``` 809 810### close 811 812close(): void 813 814释放FetchResult实例并使其失效。无法调用其他方法。 815 816**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 817 818**错误码:** 819 820接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 821 822| 错误码ID | 错误信息 | 823| -------- | ------------------------------------------------------------ | 824| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 825| 13900020 | Invalid argument. | 826| 14000011 | Internal system error | 827 828**示例:** 829 830```ts 831import { dataSharePredicates } from '@kit.ArkData'; 832import { photoAccessHelper } from '@kit.MediaLibraryKit'; 833 834async function example() { 835 console.info('fetchResultCloseDemo'); 836 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 837 let fetchOption: photoAccessHelper.FetchOptions = { 838 fetchColumns: [], 839 predicates: predicates 840 }; 841 try { 842 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 843 fetchResult.close(); 844 console.info('close succeed.'); 845 } catch (err) { 846 console.error(`close fail. error: ${err.code}, ${err.message}`); 847 } 848} 849``` 850 851### getFirstObject 852 853getFirstObject(): Promise<T> 854 855获取文件检索结果中的第一个文件资产。此方法使用Promise方式来异步返回。 856 857**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 858 859**返回值:** 860 861| 类型 | 说明 | 862| ---------------- | ------------------------------------- | 863| Promise<T> | Promise对象,返回结果集中第一个对象。 | 864 865**错误码:** 866 867接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 868 869| 错误码ID | 错误信息 | 870| -------- | ------------------------------------------------------------ | 871| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 872| 13900020 | Invalid argument. | 873| 14000011 | Internal system error | 874 875**示例:** 876 877```ts 878import { dataSharePredicates } from '@kit.ArkData'; 879import { photoAccessHelper } from '@kit.MediaLibraryKit'; 880 881async function example() { 882 console.info('getFirstObjectDemo'); 883 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 884 let fetchOption: photoAccessHelper.FetchOptions = { 885 fetchColumns: [], 886 predicates: predicates 887 }; 888 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 889 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 890 console.info('photoAsset displayName: ', photoAsset.displayName); 891} 892``` 893 894### getNextObject 895 896getNextObject(): Promise<T> 897 898获取文件检索结果中的下一个文件资产。此方法使用Promise方式来异步返回。 899在调用此方法之前,必须使用[isAfterLast()](#isafterlast)来检查当前位置是否为最后一行。 900 901**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 902 903**返回值:** 904 905| 类型 | 说明 | 906| ---------------- | ------------------------------------- | 907| Promise<T> | Promise对象,返回结果集中下一个对象。 | 908 909**错误码:** 910 911接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 912 913| 错误码ID | 错误信息 | 914| -------- | ------------------------------------------------------------ | 915| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 916| 13900020 | Invalid argument. | 917| 14000011 | Internal system error | 918 919**示例:** 920 921```ts 922import { dataSharePredicates } from '@kit.ArkData'; 923import { photoAccessHelper } from '@kit.MediaLibraryKit'; 924 925async function example() { 926 console.info('getNextObjectDemo'); 927 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 928 let fetchOption: photoAccessHelper.FetchOptions = { 929 fetchColumns: [], 930 predicates: predicates 931 }; 932 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 933 await fetchResult.getFirstObject(); 934 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getNextObject(); 935 console.info('photoAsset displayName: ', photoAsset.displayName); 936} 937``` 938 939### getLastObject 940 941getLastObject(): Promise<T> 942 943获取文件检索结果中的最后一个文件资产。此方法使用Promise方式来返回。 944 945**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 946 947**返回值:** 948 949| 类型 | 说明 | 950| ---------------- | --------------------------------------- | 951| Promise<T> | Promise对象,返回结果集中最后一个对象。 | 952 953**错误码:** 954 955接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 956 957| 错误码ID | 错误信息 | 958| -------- | ------------------------------------------------------------ | 959| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 960| 13900020 | Invalid argument. | 961| 14000011 | Internal system error | 962 963**示例:** 964 965```ts 966import { dataSharePredicates } from '@kit.ArkData'; 967import { photoAccessHelper } from '@kit.MediaLibraryKit'; 968 969async function example() { 970 console.info('getLastObjectDemo'); 971 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 972 let fetchOption: photoAccessHelper.FetchOptions = { 973 fetchColumns: [], 974 predicates: predicates 975 }; 976 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 977 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); 978 console.info('photoAsset displayName: ', photoAsset.displayName); 979} 980``` 981 982### getObjectByPosition 983 984getObjectByPosition(index: number): Promise<T> 985 986获取文件检索结果中具有指定索引的文件资产。此方法使用Promise形式返回文件Asset。 987 988**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 989 990**参数:** 991 992| 参数名 | 类型 | 必填 | 说明 | 993| ------ | ------ | ---- | ----------------------------- | 994| index | number | 是 | 要获取的文件的索引,从0开始。 | 995 996**返回值:** 997 998| 类型 | 说明 | 999| ---------------- | --------------------------------------------- | 1000| Promise<T> | Promise对象,返回结果集中指定索引的一个对象。 | 1001 1002**错误码:** 1003 1004接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1005 1006| 错误码ID | 错误信息 | 1007| -------- | ------------------------------------------------------------ | 1008| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1009| 13900020 | Invalid argument. | 1010| 14000011 | Internal system error | 1011 1012**示例:** 1013 1014```ts 1015import { dataSharePredicates } from '@kit.ArkData'; 1016import { photoAccessHelper } from '@kit.MediaLibraryKit'; 1017 1018async function example() { 1019 console.info('getObjectByPositionDemo'); 1020 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1021 let fetchOption: photoAccessHelper.FetchOptions = { 1022 fetchColumns: [], 1023 predicates: predicates 1024 }; 1025 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 1026 let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getObjectByPosition(0); 1027 console.info('photoAsset displayName: ', photoAsset.displayName); 1028} 1029``` 1030 1031### getAllObjects 1032 1033getAllObjects(): Promise<Array<T>> 1034 1035获取文件检索结果中的所有文件资产。此方法使用Promise方式来异步返回。 1036 1037**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1038 1039**返回值:** 1040 1041| 类型 | 说明 | 1042| ----------------------------- | ------------------------------------------- | 1043| Promise<Array<T>> | Promise对象,返回结果集中所有文件资产数组。 | 1044 1045**错误码:** 1046 1047接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1048 1049| 错误码ID | 错误信息 | 1050| -------- | ------------------------------------------------------------ | 1051| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1052| 13900020 | Invalid argument. | 1053| 14000011 | Internal system error | 1054 1055**示例:** 1056 1057```ts 1058import { dataSharePredicates } from '@kit.ArkData'; 1059import { photoAccessHelper } from '@kit.MediaLibraryKit'; 1060 1061async function example() { 1062 console.info('getAllObjectDemo'); 1063 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1064 let fetchOption: photoAccessHelper.FetchOptions = { 1065 fetchColumns: [], 1066 predicates: predicates 1067 }; 1068 let fetchResult: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); 1069 let photoAssetList: Array<sendablePhotoAccessHelper.PhotoAsset> = await fetchResult.getAllObjects(); 1070 console.info('photoAssetList length: ', photoAssetList.length); 1071} 1072``` 1073 1074## Album 1075 1076实体相册 1077 1078### 属性 1079 1080**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1081 1082| 名称 | 类型 | 只读 | 可选 | 说明 | 1083| ------------ | ----------------------------- | ---------------------------- | ---- | ---------------- | 1084| albumType | [AlbumType](#albumtype) | 是 | 否 | 相册类型。 | 1085| albumSubtype | [AlbumSubtype](#albumsubtype) | 是 | 否 | 相册子类型。 | 1086| albumName | string | 用户相册可写,预置相册不可写 | 否 | 相册名称。 | 1087| albumUri | string | 是 | 否 | 相册Uri。 | 1088| count | number | 是 | 否 | 相册中文件数量。 | 1089| coverUri | string | 是 | 否 | 封面文件Uri。 | 1090| imageCount | number | 是 | 是 | 相册中图片数量。 | 1091| videoCount | number | 是 | 是 | 相册中视频数量。 | 1092 1093### convertToPhotoAlbum 1094 1095convertToPhotoAlbum(): photoAccessHelper.Album 1096 1097将Sendable类型Album转换为非Sendable类型Album。 1098 1099**需要权限**:ohos.permission.READ_IMAGEVIDEO 1100 1101**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1102 1103| 类型 | 说明 | 1104| ----------------------- | --------------------------------------------------------- | 1105| photoAccessHelper.Album | 非Sendable类型[Album](js-apis-photoAccessHelper.md#album) | 1106 1107**错误码:** 1108 1109接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1110 1111| 错误码ID | 错误信息 | 1112| -------- | ------------------------------------------------------------ | 1113| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1114| 201 | Permission denied. | 1115| 13900020 | Invalid argument. | 1116| 14000011 | Internal system error | 1117 1118**示例:** 1119 1120```ts 1121import { dataSharePredicates } from '@kit.ArkData'; 1122import { BusinessError } from '@kit.BasicServicesKit'; 1123import { photoAccessHelper } from '@kit.MediaLibraryKit'; 1124 1125async function example() { 1126 console.info('convertToPhotoAlbumDemo'); 1127 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1128 let albumFetchOptions: photoAccessHelper.FetchOptions = { 1129 fetchColumns: [], 1130 predicates: predicates 1131 }; 1132 let fetchOption: photoAccessHelper.FetchOptions = { 1133 fetchColumns: [], 1134 predicates: predicates 1135 }; 1136 let albumList: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.Album> = await phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 1137 let sendableAlbum: sendablePhotoAccessHelper.Album = await albumList.getFirstObject(); 1138 let album: photoAccessHelper.Album = sendableAlbum.convertToPhotoAlbum(); 1139 album.getAssets(fetchOption).then((albumFetchResult) => { 1140 console.info('convertToPhotoAlbum successfully, getCount: ' + albumFetchResult.getCount()); 1141 }).catch((err: BusinessError) => { 1142 console.error(`convertToPhotoAlbum failed with error: ${err.code}, ${err.message}`); 1143 }); 1144} 1145``` 1146 1147### getAssets 1148 1149getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 1150 1151获取相册中的文件。该方法使用Promise来返回文件。 1152 1153**需要权限**:ohos.permission.READ_IMAGEVIDEO 1154 1155**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1156 1157**参数:** 1158 1159| 参数名 | 类型 | 必填 | 说明 | 1160| ------- | --------------------------------------------------------- | ---- | ---------- | 1161| options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 检索选项。 | 1162 1163**返回值:** 1164 1165| 类型 | 说明 | 1166| ------------------------------------------------------------ | --------------------------------------- | 1167| Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise对象,返回图片和视频数据结果集。 | 1168 1169**错误码:** 1170 1171接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1172 1173| 错误码ID | 错误信息 | 1174| -------- | ------------------------------------------------------------ | 1175| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1176| 201 | Permission denied. | 1177| 13900020 | Invalid argument. | 1178| 14000011 | Internal system error | 1179 1180**示例:** 1181 1182```ts 1183import { dataSharePredicates } from '@kit.ArkData'; 1184import { BusinessError } from '@kit.BasicServicesKit'; 1185import { photoAccessHelper } from '@kit.MediaLibraryKit'; 1186 1187async function example() { 1188 console.info('albumGetAssetsDemoPromise'); 1189 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1190 let albumFetchOptions: photoAccessHelper.FetchOptions = { 1191 fetchColumns: [], 1192 predicates: predicates 1193 }; 1194 let fetchOption: photoAccessHelper.FetchOptions = { 1195 fetchColumns: [], 1196 predicates: predicates 1197 }; 1198 let albumList: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.Album> = await phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 1199 let album: sendablePhotoAccessHelper.Album = await albumList.getFirstObject(); 1200 album.getAssets(fetchOption).then((albumFetchResult) => { 1201 console.info('album getAssets successfully, getCount: ' + albumFetchResult.getCount()); 1202 }).catch((err: BusinessError) => { 1203 console.error(`album getAssets failed with error: ${err.code}, ${err.message}`); 1204 }); 1205} 1206``` 1207 1208### commitModify 1209 1210commitModify(): Promise<void> 1211 1212更新相册属性修改到数据库中。该方法使用Promise来返回结果。 1213 1214**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 1215 1216**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1217 1218**返回值:** 1219 1220| 类型 | 说明 | 1221| ------------------- | ----------------------- | 1222| Promise<void> | Promise对象,返回void。 | 1223 1224**错误码:** 1225 1226接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1227 1228| 错误码ID | 错误信息 | 1229| -------- | ------------------------------------------------------------ | 1230| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 1231| 201 | Permission denied. | 1232| 13900020 | Invalid argument. | 1233| 14000011 | Internal system error | 1234 1235**示例:** 1236 1237```ts 1238import { dataSharePredicates } from '@kit.ArkData'; 1239import { BusinessError } from '@kit.BasicServicesKit'; 1240import { photoAccessHelper } from '@kit.MediaLibraryKit'; 1241 1242async function example() { 1243 console.info('albumCommitModifyDemo'); 1244 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1245 let albumFetchOptions: photoAccessHelper.FetchOptions = { 1246 fetchColumns: [], 1247 predicates: predicates 1248 }; 1249 let albumList: sendablePhotoAccessHelper.FetchResult<sendablePhotoAccessHelper.Album> = await phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 1250 let album: sendablePhotoAccessHelper.Album = await albumList.getFirstObject(); 1251 album.albumName = 'hello'; 1252 album.commitModify().then(() => { 1253 console.info('commitModify successfully'); 1254 }).catch((err: BusinessError) => { 1255 console.error(`commitModify failed with error: ${err.code}, ${err.message}`); 1256 }); 1257} 1258``` 1259 1260## PhotoType 1261 1262枚举,媒体文件类型。 1263 1264**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 1265 1266**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1267 1268| 名称 | 值 | 说明 | 1269| ----- | ---- | ------ | 1270| IMAGE | 1 | 图片。 | 1271| VIDEO | 2 | 视频。 | 1272 1273## AlbumType 1274 1275枚举,相册类型,表示是用户相册还是系统预置相册。 1276 1277**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1278 1279| 名称 | 值 | 说明 | 1280| ------ | ---- | -------------- | 1281| USER | 0 | 用户相册。 | 1282| SYSTEM | 1024 | 系统预置相册。 | 1283 1284## AlbumSubtype 1285 1286枚举,相册子类型,表示具体的相册类型。 1287 1288**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1289 1290| 名称 | 值 | 说明 | 1291| ------------- | ---------- | ---------- | 1292| USER\_GENERIC | 1 | 用户相册。 | 1293| FAVORITE | 1025 | 收藏夹。 | 1294| VIDEO | 1026 | 视频相册。 | 1295| IMAGE | 1031 | 图片相册。 | 1296| ANY | 2147483647 | 任意相册。 | 1297