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