1# Interface (PhotoAccessHelper) 2 3<!--Kit: Media Library Kit--> 4<!--Subsystem: Multimedia--> 5<!--Owner: @xuchangda; @yixiaoff--> 6<!--Designer: @guxinggang; @liweilu1--> 7<!--Tester: @wangbeibei; @xchaosioda--> 8<!--Adviser: @zengyawen--> 9 10> **说明:** 11> 12> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 13 14## 导入模块 15 16```ts 17import { photoAccessHelper } from '@kit.MediaLibraryKit'; 18``` 19 20## getAssets 21 22getAssets(options: FetchOptions, callback: AsyncCallback<FetchResult<PhotoAsset>>): void 23 24获取图片和视频资源,使用callback方式返回结果。 25 26**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 27 28**需要权限**:ohos.permission.READ_IMAGEVIDEO 29 30 通过picker的方式调用该接口来查询指定uri对应的图片或视频资源,不需要申请'ohos.permission.READ_IMAGEVIDEO'权限,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-photoviewpicker.md#指定uri获取图片或视频资源)。 31 32**参数:** 33 34| 参数名 | 类型 | 必填 | 说明 | 35| -------- | ------------------------ | ---- | ------------------------- | 36| options | [FetchOptions](arkts-apis-photoAccessHelper-i.md#fetchoptions) | 是 | 图片和视频检索选项。 | 37| callback | AsyncCallback<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[PhotoAsset](arkts-apis-photoAccessHelper-PhotoAsset.md)>> | 是 | callback返回图片和视频检索结果集。 | 38 39**错误码:** 40 41接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 42 43在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 44 45| 错误码ID | 错误信息 | 46| -------- | ---------------------------------------- | 47| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 48| 201 | Permission denied. | 49| 13900020 | Invalid argument. | 50| 14000011 | System inner fail. | 51 52**示例:** 53 54phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 55 56```ts 57import { dataSharePredicates } from '@kit.ArkData'; 58 59async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 60 console.info('getAssets'); 61 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 62 let fetchOptions: photoAccessHelper.FetchOptions = { 63 fetchColumns: [], 64 predicates: predicates 65 }; 66 67 phAccessHelper.getAssets(fetchOptions, async (err, fetchResult) => { 68 if (fetchResult !== undefined) { 69 console.info('fetchResult success'); 70 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 71 if (photoAsset !== undefined) { 72 console.info('photoAsset.displayName : ' + photoAsset.displayName); 73 } 74 } else { 75 console.error(`fetchResult fail with error: ${err.code}, ${err.message}`); 76 } 77 }); 78} 79``` 80 81## getAssets 82 83getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 84 85获取图片和视频资源,使用Promise方式返回结果。 86 87**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 88 89**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 90 91**需要权限**:ohos.permission.READ_IMAGEVIDEO 92 93 通过picker的方式调用该接口来查询指定uri对应的图片或视频资源,不需要申请'ohos.permission.READ_IMAGEVIDEO'权限,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-photoviewpicker.md#指定uri获取图片或视频资源)。 94 95**参数:** 96 97| 参数名 | 类型 | 必填 | 说明 | 98| ------- | ------------------- | ---- | ---------------- | 99| options | [FetchOptions](arkts-apis-photoAccessHelper-i.md#fetchoptions) | 是 | 图片和视频检索选项。 | 100 101**返回值:** 102 103| 类型 | 说明 | 104| --------------------------- | -------------- | 105| Promise<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[PhotoAsset](arkts-apis-photoAccessHelper-PhotoAsset.md)>> | Promise对象,返回图片和视频数据结果集。 | 106 107**错误码:** 108 109接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 110 111在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 112 113| 错误码ID | 错误信息 | 114| -------- | ---------------------------------------- | 115| 201 | Permission denied. | 116| 13900020 | Invalid argument. | 117| 14000011 | System inner fail. | 118 119**示例:** 120 121phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 122 123```ts 124import { dataSharePredicates } from '@kit.ArkData'; 125 126async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 127 console.info('getAssets'); 128 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 129 let fetchOptions: photoAccessHelper.FetchOptions = { 130 fetchColumns: [], 131 predicates: predicates 132 }; 133 try { 134 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 135 if (fetchResult !== undefined) { 136 console.info('fetchResult success'); 137 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 138 if (photoAsset !== undefined) { 139 console.info('photoAsset.displayName :' + photoAsset.displayName); 140 } 141 } 142 } catch (err) { 143 console.error(`getAssets failed, error: ${err.code}, ${err.message}`); 144 } 145} 146``` 147 148## getBurstAssets<sup>12+</sup> 149 150getBurstAssets(burstKey: string, options: FetchOptions): Promise<FetchResult<PhotoAsset>> 151 152获取连拍照片资源,使用Promise方式返回结果。 153 154**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 155 156**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 157 158**需要权限**:ohos.permission.READ_IMAGEVIDEO 159 160**参数:** 161 162| 参数名 | 类型 | 必填 | 说明 | 163| ------- | ------------------- | ---- | ---------------- | 164| burstKey | string | 是 | 一组连拍照片的唯一标识:uuid(可传入[PhotoKeys](arkts-apis-photoAccessHelper-e.md#photokeys)的BURST_KEY)。字符串长度为36。 | 165| options | [FetchOptions](arkts-apis-photoAccessHelper-i.md#fetchoptions) | 是 | 连拍照片检索选项。 | 166 167**返回值:** 168 169| 类型 | 说明 | 170| --------------------------- | -------------- | 171| Promise<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[PhotoAsset](arkts-apis-photoAccessHelper-PhotoAsset.md)>> | Promise对象,返回连拍照片数据结果集。 | 172 173**错误码:** 174 175接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 176 177| 错误码ID | 错误信息 | 178| -------- | ---------------------------------------- | 179| 201 | Permission denied. | 180| 14000011 | Internal system error. | 181 182**示例:** 183 184phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 185 186```ts 187import { dataSharePredicates } from '@kit.ArkData'; 188 189async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 190 console.info('getBurstAssets'); 191 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 192 let fetchOptions: photoAccessHelper.FetchOptions = { 193 fetchColumns: [], 194 predicates: predicates 195 }; 196 // burstKey为36位的uuid,可以根据photoAccessHelper.PhotoKeys获取。 197 let burstKey: string = "e719d696-09fa-44f8-8e9e-ec3f215aa62a"; 198 try { 199 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await 200 phAccessHelper.getBurstAssets(burstKey, fetchOptions); 201 if (fetchResult !== undefined) { 202 console.info('fetchResult success'); 203 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 204 if (photoAsset !== undefined) { 205 console.info('photoAsset.displayName :' + photoAsset.displayName); 206 } 207 } 208 } catch (err) { 209 console.error(`getBurstAssets failed, error: ${err.code}, ${err.message}`); 210 } 211} 212``` 213 214## createAsset 215 216createAsset(photoType: PhotoType, extension: string, options: CreateOptions, callback: AsyncCallback<string>): void 217 218指定文件类型、后缀和创建选项,创建图片或视频资源。使用callback方式返回结果。 219 220在未申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'时,可以使用安全控件或授权弹窗的方式创建媒体资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-savebutton.md)。 221 222**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 223 224**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 225 226**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 227 228**参数:** 229 230| 参数名 | 类型 | 必填 | 说明 | 231| -------- | ------------------------ | ---- | ------------------------- | 232| photoType | [PhotoType](arkts-apis-photoAccessHelper-e.md#phototype) | 是 | 创建的文件类型,IMAGE或者VIDEO类型。 | 233| extension | string | 是 | 文件名后缀参数,例如:'jpg'。 | 234| options | [CreateOptions](arkts-apis-photoAccessHelper-i.md#createoptions) | 是 | 创建选项,当前仅支持'title',例如{title: 'testPhoto'}。<br>**注意:** 传入其他选项,配置不生效。<br>文件名中不允许出现非法英文字符,包括: . .. \ / : * ? " ' ` < > \| { } [ ] | 235| callback | AsyncCallback<string> | 是 | callback返回创建的图片和视频的uri。 | 236 237**错误码:** 238 239接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 240 241在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 242 243| 错误码ID | 错误信息 | 244| -------- | ---------------------------------------- | 245| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 246| 201 | Permission denied. | 247| 13900020 | Invalid argument. | 248| 14000011 | System inner fail. | 249 250**示例:** 251 252phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 253 254```ts 255async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 256 console.info('createAssetDemo'); 257 let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; 258 let extension:string = 'jpg'; 259 let options: photoAccessHelper.CreateOptions = { 260 title: 'testPhoto' 261 } 262 phAccessHelper.createAsset(photoType, extension, options, (err, uri) => { 263 if (uri !== undefined) { 264 console.info('createAsset uri' + uri); 265 console.info('createAsset successfully'); 266 } else { 267 console.error(`createAsset failed, error: ${err.code}, ${err.message}`); 268 } 269 }); 270} 271``` 272 273## createAsset 274 275createAsset(photoType: PhotoType, extension: string, callback: AsyncCallback<string>): void 276 277指定文件类型和后缀,创建图片或视频资源,使用callback方式返回结果。 278 279在未申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'时,可以使用安全控件或授权弹窗的方式创建媒体资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-savebutton.md)。 280 281**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 282 283**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 284 285**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 286 287**参数:** 288 289| 参数名 | 类型 | 必填 | 说明 | 290| -------- | ------------------------ | ---- | ------------------------- | 291| photoType | [PhotoType](arkts-apis-photoAccessHelper-e.md#phototype) | 是 | 创建的文件类型,IMAGE或者VIDEO类型。 | 292| extension | string | 是 | 文件名后缀参数,例如:'jpg'。 | 293| callback | AsyncCallback<string> | 是 | callback返回创建的图片和视频的uri。 | 294 295**错误码:** 296 297接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 298 299在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 300 301| 错误码ID | 错误信息 | 302| -------- | ---------------------------------------- | 303| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 304| 201 | Permission denied. | 305| 13900020 | Invalid argument. | 306| 14000011 | System inner fail. | 307 308**示例:** 309 310phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 311 312```ts 313async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 314 console.info('createAssetDemo'); 315 let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; 316 let extension: string = 'jpg'; 317 phAccessHelper.createAsset(photoType, extension, (err, uri) => { 318 if (uri !== undefined) { 319 console.info('createAsset uri' + uri); 320 console.info('createAsset successfully'); 321 } else { 322 console.error(`createAsset failed, error: ${err.code}, ${err.message}`); 323 } 324 }); 325} 326``` 327 328## createAsset 329 330createAsset(photoType: PhotoType, extension: string, options?: CreateOptions): Promise<string> 331 332指定文件类型、后缀和创建选项,创建图片或视频资源,以Promise方式返回结果。 333 334在未申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'时,可以使用安全控件或授权弹窗的方式创建媒体资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-savebutton.md)。 335 336**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 337 338**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 339 340**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 341 342**参数:** 343 344| 参数名 | 类型 | 必填 | 说明 | 345| -------- | ------------------------ | ---- | ------------------------- | 346| photoType | [PhotoType](arkts-apis-photoAccessHelper-e.md#phototype) | 是 | 创建的文件类型,IMAGE或者VIDEO类型。 | 347| extension | string | 是 | 文件名后缀参数,例如:'jpg'。 | 348| options | [CreateOptions](arkts-apis-photoAccessHelper-i.md#createoptions) | 否 | 创建选项,当前仅支持'title',例如{title: 'testPhoto'}。<br>**注意:** 传入其他选项,配置不生效。<br>文件名中不允许出现非法英文字符,包括: . .. \ / : * ? " ' ` < > \| { } [ ] | 349 350**返回值:** 351 352| 类型 | 说明 | 353| --------------------------- | -------------- | 354| Promise<string> | Promise对象,返回创建的图片和视频的uri。 | 355 356**错误码:** 357 358接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 359 360在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 361 362| 错误码ID | 错误信息 | 363| -------- | ---------------------------------------- | 364| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 365| 201 | Permission denied. | 366| 13900020 | Invalid argument. | 367| 14000011 | System inner fail. | 368 369**示例:** 370 371phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 372 373```ts 374async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 375 console.info('createAssetDemo'); 376 try { 377 let photoType: photoAccessHelper.PhotoType = photoAccessHelper.PhotoType.IMAGE; 378 let extension: string = 'jpg'; 379 let options: photoAccessHelper.CreateOptions = { 380 title: 'testPhoto' 381 } 382 let uri: string = await phAccessHelper.createAsset(photoType, extension, options); 383 console.info('createAsset uri' + uri); 384 console.info('createAsset successfully'); 385 } catch (err) { 386 console.error(`createAsset failed, error: ${err.code}, ${err.message}`); 387 } 388} 389``` 390 391## getAlbums 392 393getAlbums(type: AlbumType, subtype: AlbumSubtype, options: FetchOptions, callback: AsyncCallback<FetchResult<Album>>): void 394 395根据检索选项和相册类型获取相册,使用callback方式返回结果。 396 397获取相册前,确保相册已存在。 398 399**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 400 401**需要权限**:ohos.permission.READ_IMAGEVIDEO 402 403**参数:** 404 405| 参数名 | 类型 | 必填 | 说明 | 406| -------- | ------------------------ | ---- | ------------------------- | 407| type | [AlbumType](arkts-apis-photoAccessHelper-e.md#albumtype) | 是 | 相册类型。 | 408| subtype | [AlbumSubtype](arkts-apis-photoAccessHelper-e.md#albumsubtype) | 是 | 相册子类型。 | 409| options | [FetchOptions](arkts-apis-photoAccessHelper-i.md#fetchoptions) | 是 | 检索选项。 | 410| callback | AsyncCallback<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[Album](arkts-apis-photoAccessHelper-Album.md)>> | 是 | callback返回获取相册的结果集。 | 411 412**错误码:** 413 414接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 415 416在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 417 418| 错误码ID | 错误信息 | 419| -------- | ---------------------------------------- | 420| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 421| 201 | Permission denied. | 422| 13900020 | Invalid argument. | 423| 14000011 | System inner fail. | 424 425**示例:** 426 427phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 428 429```ts 430import { dataSharePredicates } from '@kit.ArkData'; 431 432async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 433 // 示例代码中为获取相册名为newAlbumName的相册。 434 console.info('getAlbumsDemo'); 435 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 436 predicates.equalTo('album_name', 'newAlbumName'); 437 let fetchOptions: photoAccessHelper.FetchOptions = { 438 fetchColumns: [], 439 predicates: predicates 440 }; 441 phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions, async (err, fetchResult) => { 442 if (err) { 443 console.error(`getAlbumsCallback failed with err: ${err.code}, ${err.message}`); 444 return; 445 } 446 if (fetchResult === undefined) { 447 console.error('getAlbumsCallback fetchResult is undefined'); 448 return; 449 } 450 let album = await fetchResult.getFirstObject(); 451 console.info('getAlbumsCallback successfully, albumName: ' + album.albumName); 452 fetchResult.close(); 453 }); 454} 455``` 456 457## getAlbums 458 459getAlbums(type: AlbumType, subtype: AlbumSubtype, callback: AsyncCallback<FetchResult<Album>>): void 460 461根据相册类型获取相册,使用callback方式返回结果。 462 463获取相册前需先保证相册存在。 464 465**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 466 467**需要权限**:ohos.permission.READ_IMAGEVIDEO 468 469**参数:** 470 471| 参数名 | 类型 | 必填 | 说明 | 472| -------- | ------------------------ | ---- | ------------------------- | 473| type | [AlbumType](arkts-apis-photoAccessHelper-e.md#albumtype) | 是 | 相册类型。 | 474| subtype | [AlbumSubtype](arkts-apis-photoAccessHelper-e.md#albumsubtype) | 是 | 相册子类型。 | 475| callback | AsyncCallback<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[Album](arkts-apis-photoAccessHelper-Album.md)>> | 是 | callback返回获取相册的结果集。 | 476 477**错误码:** 478 479接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 480 481在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 482 483| 错误码ID | 错误信息 | 484| -------- | ---------------------------------------- | 485| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 486| 201 | Permission denied. | 487| 13900020 | Invalid argument. | 488| 14000011 | System inner fail. | 489 490**示例:** 491 492phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 493 494```ts 495async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 496 // 示例代码中为获取统相册VIDEO,默认已预置。 497 console.info('getAlbumsDemo'); 498 phAccessHelper.getAlbums(photoAccessHelper.AlbumType.SYSTEM, photoAccessHelper.AlbumSubtype.VIDEO, async (err, fetchResult) => { 499 if (err) { 500 console.error(`getAlbumsCallback failed with err: ${err.code}, ${err.message}`); 501 return; 502 } 503 if (fetchResult === undefined) { 504 console.error('getAlbumsCallback fetchResult is undefined'); 505 return; 506 } 507 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 508 console.info('getAlbumsCallback successfully, albumUri: ' + album.albumUri); 509 fetchResult.close(); 510 }); 511} 512``` 513 514## getAlbums 515 516getAlbums(type: AlbumType, subtype: AlbumSubtype, options?: FetchOptions): Promise<FetchResult<Album>> 517 518根据检索选项和相册类型获取相册,使用Promise方式返回结果。 519 520在获取相册之前,确保相册已存在。 521 522**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 523 524**需要权限**:ohos.permission.READ_IMAGEVIDEO 525 526**参数:** 527 528| 参数名 | 类型 | 必填 | 说明 | 529| -------- | ------------------------ | ---- | ------------------------- | 530| type | [AlbumType](arkts-apis-photoAccessHelper-e.md#albumtype) | 是 | 相册类型。 | 531| subtype | [AlbumSubtype](arkts-apis-photoAccessHelper-e.md#albumsubtype) | 是 | 相册子类型。 | 532| options | [FetchOptions](arkts-apis-photoAccessHelper-i.md#fetchoptions) | 否 | 检索选项,不填时默认根据相册类型检索。 | 533 534**返回值:** 535 536| 类型 | 说明 | 537| --------------------------- | -------------- | 538| Promise<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[Album](arkts-apis-photoAccessHelper-Album.md)>> | Promise对象,返回获取相册的结果集。 | 539 540**错误码:** 541 542接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 543 544错误码13900012,请参考[开发准备](../../media/medialibrary/photoAccessHelper-preparation.md)。 545 546| 错误码ID | 错误信息 | 547| -------- | ---------------------------------------- | 548| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 549| 201 | Permission denied. | 550| 13900020 | Invalid argument. | 551| 14000011 | System inner fail. | 552 553**示例:** 554 555phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 556 557```ts 558import { dataSharePredicates } from '@kit.ArkData'; 559import { BusinessError } from '@kit.BasicServicesKit'; 560 561async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 562 // 示例代码中为获取相册名为newAlbumName的相册。 563 console.info('getAlbumsDemo'); 564 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 565 predicates.equalTo('album_name', 'newAlbumName'); 566 let fetchOptions: photoAccessHelper.FetchOptions = { 567 fetchColumns: [], 568 predicates: predicates 569 }; 570 phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions).then( async (fetchResult) => { 571 if (fetchResult === undefined) { 572 console.error('getAlbumsPromise fetchResult is undefined'); 573 return; 574 } 575 let album: photoAccessHelper.Album = await fetchResult.getFirstObject(); 576 console.info('getAlbumsPromise successfully, albumName: ' + album.albumName); 577 fetchResult.close(); 578 }).catch((err: BusinessError) => { 579 console.error(`getAlbumsPromise failed with err: ${err.code}, ${err.message}`); 580 }); 581} 582``` 583 584## registerChange 585 586registerChange(uri: string, forChildUris: boolean, callback: Callback<ChangeData>) : void 587 588注册指定uri的监听,并通过callback方式返回异步结果。 589 590**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 591 592**参数:** 593 594| 参数名 | 类型 | 必填 | 说明 | 595| --------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 596| uri | string | 是 | PhotoAsset的uri, Album的uri或[DefaultChangeUri](arkts-apis-photoAccessHelper-e.md#defaultchangeuri)的值。 | 597| forChildUris | boolean | 是 | 是否模糊监听。uri为相册uri时:forChildUris为true,能监听到相册中文件的变化。如果是false,只能监听相册本身变化;uri为photoAsset时:forChildUris为true、false没有区别;uri为DefaultChangeUri时:forChildUris必须为true,如果为false将找不到该uri,收不到任何消息。 | 598| callback | Callback<[ChangeData](arkts-apis-photoAccessHelper-i.md#changedata)> | 是 | 返回要监听的[ChangeData](arkts-apis-photoAccessHelper-i.md#changedata)。注:uri可以注册多个不同的callback监听,[unRegisterChange](#unregisterchange)可以关闭该uri所有监听,也可以关闭指定callback的监听。 | 599 600**错误码:** 601 602接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 603 604错误码13900012,请参考[开发准备](../../media/medialibrary/photoAccessHelper-preparation.md)。 605 606| 错误码ID | 错误信息 | 607| -------- | ---------------------------------------- | 608| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 609| 13900012 | Permission denied. | 610| 13900020 | Invalid argument. | 611 612**示例:** 613 614phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 615 616```ts 617import { dataSharePredicates } from '@kit.ArkData'; 618 619async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 620 console.info('registerChangeDemo'); 621 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 622 let fetchOptions: photoAccessHelper.FetchOptions = { 623 fetchColumns: [], 624 predicates: predicates 625 }; 626 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 627 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 628 if (photoAsset !== undefined) { 629 console.info('photoAsset.displayName : ' + photoAsset.displayName); 630 } 631 let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { 632 console.info('onCallback1 success, changData: ' + JSON.stringify(changeData)); 633 //file had changed, do something. 634 } 635 let onCallback2 = (changeData: photoAccessHelper.ChangeData) => { 636 console.info('onCallback2 success, changData: ' + JSON.stringify(changeData)); 637 //file had changed, do something. 638 } 639 // 注册onCallback1监听。 640 phAccessHelper.registerChange(photoAsset.uri, false, onCallback1); 641 // 注册onCallback2监听。 642 phAccessHelper.registerChange(photoAsset.uri, false, onCallback2); 643 644 await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]); 645} 646``` 647 648## unRegisterChange 649 650unRegisterChange(uri: string, callback?: Callback<ChangeData>): void 651 652取消指定uri的监听,一个uri可以注册多个监听,存在多个callback监听时,可以取消指定注册的callback的监听;不指定callback时取消该uri的所有监听。 653 654**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 655 656**参数:** 657 658| 参数名 | 类型 | 必填 | 说明 | 659| -------- | ------------------------------------------- | ---- | ------------------------------------------------------------ | 660| uri | string | 是 | PhotoAsset的uri, Album的uri或[DefaultChangeUri](arkts-apis-photoAccessHelper-e.md#defaultchangeuri)的值。 | 661| callback | Callback<[ChangeData](arkts-apis-photoAccessHelper-i.md#changedata)> | 否 | 取消[registerChange](#registerchange)注册时的callback的监听,不填时,取消该uri的所有监听。注:off指定注册的callback后不会进入此回调。 | 662 663**错误码:** 664 665接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 666 667错误码13900012,请参考[开发准备](../../media/medialibrary/photoAccessHelper-preparation.md)。 668 669| 错误码ID | 错误信息 | 670| -------- | ---------------------------------------- | 671| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 672| 13900012 | Permission denied. | 673| 13900020 | Invalid argument. | 674 675**示例:** 676 677phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 678 679```ts 680import { dataSharePredicates } from '@kit.ArkData'; 681 682async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 683 console.info('offDemo'); 684 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 685 let fetchOptions: photoAccessHelper.FetchOptions = { 686 fetchColumns: [], 687 predicates: predicates 688 }; 689 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 690 let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 691 if (photoAsset !== undefined) { 692 console.info('photoAsset.displayName : ' + photoAsset.displayName); 693 } 694 let onCallback1 = (changeData: photoAccessHelper.ChangeData) => { 695 console.info('onCallback1 on'); 696 } 697 let onCallback2 = (changeData: photoAccessHelper.ChangeData) => { 698 console.info('onCallback2 on'); 699 } 700 // 注册onCallback1监听。 701 phAccessHelper.registerChange(photoAsset.uri, false, onCallback1); 702 // 注册onCallback2监听。 703 phAccessHelper.registerChange(photoAsset.uri, false, onCallback2); 704 // 关闭onCallback1监听,onCallback2 继续监听。 705 phAccessHelper.unRegisterChange(photoAsset.uri, onCallback1); 706 await photoAccessHelper.MediaAssetChangeRequest.deleteAssets(context, [photoAsset]); 707} 708``` 709 710## applyChanges<sup>11+</sup> 711 712applyChanges(mediaChangeRequest: MediaChangeRequest): Promise<void> 713 714提交媒体变更请求,使用Promise方式返回结果。 715 716**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 717 718在未申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'时,可以使用安全控件或授权弹窗的方式创建媒体资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-savebutton.md)。 719 720**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 721 722**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 723 724**参数:** 725 726| 参数名 | 类型 | 必填 | 说明 | 727| -------- | ------------------------ | ---- | ------------------------- | 728| mediaChangeRequest | [MediaChangeRequest](arkts-apis-photoAccessHelper-i.md#mediachangerequest11) | 是 | 媒体变更请求,支持资产变更请求和相册变更请求。 | 729 730**返回值:** 731 732| 类型 | 说明 | 733| --------------------------------------- | ----------------- | 734| Promise<void>| Promise对象,返回void。 | 735 736**错误码:** 737 738接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 739 740| 错误码ID | 错误信息 | 741| -------- | ---------------------------------------- | 742| 201 | Permission denied. | 743| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 744| 14000011 | System inner fail. | 745 746**示例:** 747 748该接口依赖于[MediaChangeRequest](arkts-apis-photoAccessHelper-i.md#mediachangerequest11)对象,详细代码示例请参见[MediaAssetChangeRequest](arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md)和[MediaAlbumChangeRequest](arkts-apis-photoAccessHelper-MediaAlbumChangeRequest.md)中的接口示例。 749 750## release 751 752release(callback: AsyncCallback<void>): void 753 754释放PhotoAccessHelper实例,使用callback方式返回结果。 755当后续不需要使用PhotoAccessHelper实例中的方法时调用。 756 757**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 758 759**参数:** 760 761| 参数名 | 类型 | 必填 | 说明 | 762| -------- | ------------------------- | ---- | -------------------- | 763| callback | AsyncCallback<void> | 是 | 回调表示成功还是失败。 | 764 765**错误码:** 766 767接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 768 769| 错误码ID | 错误信息 | 770| -------- | ---------------------------------------- | 771| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 772| 13900020 | Invalid argument. | 773| 14000011 | System inner fail. | 774 775**示例:** 776 777phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 778 779```ts 780async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 781 console.info('releaseDemo'); 782 phAccessHelper.release((err) => { 783 if (err !== undefined) { 784 console.error(`release failed. error: ${err.code}, ${err.message}`); 785 } else { 786 console.info('release ok.'); 787 } 788 }); 789} 790``` 791 792## release 793 794release(): Promise<void> 795 796释放PhotoAccessHelper实例,使用Promise方式返回结果。 797当后续不需要使用PhotoAccessHelper 实例中的方法时调用。 798 799**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 800 801**返回值:** 802 803| 类型 | 说明 | 804| ------------------- | --------------------------------- | 805| Promise<void> | Promise对象,返回void。 | 806 807**错误码:** 808 809接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 810 811| 错误码ID | 错误信息 | 812| -------- | ---------------------------------------- | 813| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 814| 13900020 | Invalid argument. | 815| 14000011 | System inner fail. | 816 817**示例:** 818 819phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 820 821```ts 822async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 823 console.info('releaseDemo'); 824 try { 825 await phAccessHelper.release(); 826 console.info('release ok.'); 827 } catch (err) { 828 console.error(`release failed. error: ${err.code}, ${err.message}`); 829 } 830} 831``` 832 833## showAssetsCreationDialog<sup>12+</sup> 834 835showAssetsCreationDialog(srcFileUris: Array<string>, photoCreationConfigs: Array<PhotoCreationConfig>): Promise<Array<string>> 836 837调用接口拉起保存确认弹窗。用户同意保存后,返回已创建并授予保存权限的uri列表,该列表永久生效,应用可使用该uri写入图片/视频。如果用户拒绝保存,将返回空列表。弹框需要显示应用名称,无法直接获取应用名称,依赖于配置项的label和icon,因此调用此接口时请确保module.json5文件中的abilities标签中配置了label和icon项。 838 839> **说明:** 840> 当传入uri为沙箱路径时,可正常保存图片/视频,但无界面预览。 841 842**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 843 844**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 845 846**参数:** 847 848| 参数名 | 类型 | 必填 | 说明 | 849| -------- |----------------------------------------------------------------------| ---- | ------------------------- | 850| srcFileUris | Array<string> | 是 | 需保存到媒体库中的图片/视频文件对应的[媒体库uri](../../file-management/user-file-uri-intro.md#媒体文件uri)。<br>**注意:**<br>- 一次弹窗最多保存100张图片。<br>- 仅支持处理图片、视频uri。<br>- 不支持手动拼接的uri,需调用接口获取,获取方式参考[媒体文件uri获取方式](../../file-management/user-file-uri-intro.md#媒体文件uri获取方式)。 | 851| photoCreationConfigs | Array<[PhotoCreationConfig](arkts-apis-photoAccessHelper-i.md#photocreationconfig12)> | 是 | 保存图片或视频到媒体库的配置,包括文件名等,与srcFileUris保持一一对应。 | 852 853**返回值:** 854 855| 类型 | 说明 | 856| --------------------------------------- | ----------------- | 857| Promise<Array<string>> | Promise对象,返回给应用的媒体库文件uri列表。uri已对应用授权,支持应用写入数据。如果生成uri异常,则返回批量创建错误码。<br>返回-3006表示不允许出现非法字符;返回-2004表示图片类型和后缀不符;返回-203表示文件操作异常。 | 858 859**错误码:** 860 861接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 862 863| 错误码ID | 错误信息 | 864| -------- | ---------------------------------------- | 865| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 866| 14000011 | Internal system error. | 867 868**示例:** 869 870phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 871 872```ts 873import { dataSharePredicates } from '@kit.ArkData'; 874 875async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 876 console.info('ShowAssetsCreationDialogDemo.'); 877 878 try { 879 // 获取需要保存到媒体库的位于应用沙箱的图片/视频uri。 880 let srcFileUris: Array<string> = [ 881 'file://fileUriDemo1' // 实际场景请使用真实的uri。 882 ]; 883 let photoCreationConfigs: Array<photoAccessHelper.PhotoCreationConfig> = [ 884 { 885 title: 'test2', // 可选。 886 fileNameExtension: 'jpg', 887 photoType: photoAccessHelper.PhotoType.IMAGE, 888 subtype: photoAccessHelper.PhotoSubtype.DEFAULT, // 可选。 889 } 890 ]; 891 let desFileUris: Array<string> = await phAccessHelper.showAssetsCreationDialog(srcFileUris, photoCreationConfigs); 892 console.info('showAssetsCreationDialog success, data is ' + desFileUris); 893 } catch (err) { 894 console.error('showAssetsCreationDialog failed, errCode is ' + err.code + ', errMsg is ' + err.message); 895 } 896} 897``` 898 899## createAssetWithShortTermPermission<sup>12+</sup> 900 901createAssetWithShortTermPermission(photoCreationConfig: PhotoCreationConfig): Promise<string> 902 903接口提供给应用调用,支持首次调用后拉起保存确认弹框。在用户同意保存后返回已创建并授予保存权限的uri,支持应用使用uri写入图片/视频; 904在用户"同意"后的5min之内,同一个应用再次调用接口,支持无需弹框确认自动返回已授权的uri给应用,支持应用保存图片/视频。 905 906**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 907 908**需要权限:** ohos.permission.SHORT_TERM_WRITE_IMAGEVIDEO 909 910**参数:** 911 912| 参数名 | 类型 | 必填 | 说明 | 913| -------- |----------------------------------------------------------------------| ---- | ------------------------- | 914| photoCreationConfig | [PhotoCreationConfig](arkts-apis-photoAccessHelper-i.md#photocreationconfig12); | 是 | 保存图片/视频到媒体库的配置,包括保存的文件名等。 | 915 916**返回值:** 917 918| 类型 | 说明 | 919| --------------------------------------- | ----------------- | 920| Promise<string> | Promise对象,返回给应用的媒体库文件uri。uri已对应用授权,支持应用写入数据。如果生成uri异常,则返回批量创建错误码。<br>返回-3006表示不允许出现非法字符;返回-2004表示图片类型和后缀不符;返回-203表示文件操作异常。 | 921 922**错误码:** 923 924接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 925 926| 错误码ID | 错误信息 | 927| -------- | ---------------------------------------- | 928| 201 | Permission denied | 929| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 930| 14000011 | Internal system error | 931 932**示例:** 933 934phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 935 936```ts 937import { fileIo } from '@kit.CoreFileKit'; 938 939async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 940 console.info('createAssetWithShortTermPermissionDemo.'); 941 942 try { 943 let photoCreationConfig: photoAccessHelper.PhotoCreationConfig = { 944 title: '123456', 945 fileNameExtension: 'jpg', 946 photoType: photoAccessHelper.PhotoType.IMAGE, 947 subtype: photoAccessHelper.PhotoSubtype.DEFAULT, 948 }; 949 950 let resultUri: string = await phAccessHelper.createAssetWithShortTermPermission(photoCreationConfig); 951 let resultFile: fileIo.File = fileIo.openSync(resultUri, fileIo.OpenMode.READ_WRITE); 952 // 实际场景请使用真实的uri和文件大小。 953 let srcFile: fileIo.File = fileIo.openSync("file://test.jpg", fileIo.OpenMode.READ_ONLY); 954 let bufSize: number = 2000000; 955 let readSize: number = 0; 956 let buf = new ArrayBuffer(bufSize); 957 let readLen = fileIo.readSync(srcFile.fd, buf, { 958 offset: readSize, 959 length: bufSize 960 }); 961 if (readLen > 0) { 962 readSize += readLen; 963 fileIo.writeSync(resultFile.fd, buf, { length: readLen }); 964 } 965 fileIo.closeSync(srcFile); 966 fileIo.closeSync(resultFile); 967 } catch (err) { 968 console.error('createAssetWithShortTermPermission failed, errCode is ' + err.code + ', errMsg is ' + err.message); 969 } 970 971} 972``` 973 974## requestPhotoUrisReadPermission<sup>14+</sup> 975 976requestPhotoUrisReadPermission(srcFileUris: Array<string>): Promise<Array<string>> 977 978<!--RP1--><!--RP1End-->调用接口给未授权的uri进行授权,返回已创建并授予保存权限的uri列表。 979 980**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 981 982**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 983 984**参数:** 985 986| 参数名 | 类型 | 必填 | 说明 | 987| -------- |----------------------------------------------------------------------| ---- | ------------------------- | 988| srcFileUris | Array<string> | 是 | 需进行授权的图片/视频文件对应的[媒体库uri](../../file-management/user-file-uri-intro.md#媒体文件uri)。<br>**注意:** 仅支持处理图片、视频uri。| 989 990**返回值:** 991 992| 类型 | 说明 | 993| --------------------------------------- | ----------------- | 994| Promise<Array<string>> | Promise对象,返回已授权的uri列表。 | 995 996**错误码:** 997 998接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 999 1000| 错误码ID | 错误信息 | 1001| -------- | ---------------------------------------- | 1002| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1003| 14000011 | Internal system error | 1004 1005**示例:** 1006 1007phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1008 1009```ts 1010import { dataSharePredicates } from '@kit.ArkData'; 1011 1012async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context) { 1013 console.info('requestPhotoUrisReadPermissionDemo.'); 1014 1015 try { 1016 let phAccessHelper: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 1017 // 获取需要进行授权的图片/视频uri。 1018 let srcFileUris: Array<string> = [ 1019 'file://fileUriDemo1' // 实际场景请使用真实的uri。 1020 ]; 1021 let desFileUris: Array<string> = await phAccessHelper.requestPhotoUrisReadPermission(srcFileUris); 1022 console.info('requestPhotoUrisReadPermission success, data is ' + desFileUris); 1023 } catch (err) { 1024 console.error('requestPhotoUrisReadPermission failed, errCode is ' + err.code + ', errMsg is ' + err.message); 1025 } 1026} 1027``` 1028 1029## getSupportedPhotoFormats<sup>18+</sup> 1030 1031getSupportedPhotoFormats(photoType: PhotoType): Promise<Array<string>> 1032 1033接口提供给应用调用,获取媒体库支持的图片或者视频后缀列表。 1034 1035**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1036 1037**参数:** 1038 1039| 参数名 | 类型 | 必填 | 说明 | 1040|-----------|-------------------------|-----------|-----------------| 1041| photoType | [PhotoType](arkts-apis-photoAccessHelper-e.md#phototype) | 是 | 媒体文件类型。 | 1042 1043**返回值:** 1044 1045| 类型 | 说明 | 1046|------------------------------------------|-------------------------| 1047| Promise<Array<string>> | Promise对象,返回支持的图片或者视频后缀列表。 | 1048 1049**错误码:** 1050 1051接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1052 1053| 错误码ID | 错误信息 | 1054| -------- | ---------------------------------------- | 1055| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1056| 14000011 | Internal system error. It is recommended to retry and check the logs. | 1057 1058**示例:** 1059 1060phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1061 1062```ts 1063async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, photoTypeNumber: number){ 1064 console.info('getSupportedPhotoFormatsDemo.'); 1065 1066 try { 1067 let outputText: string; 1068 if (photoTypeNumber !== photoAccessHelper.PhotoType.IMAGE && photoTypeNumber !== photoAccessHelper.PhotoType.VIDEO) { 1069 outputText = 'Does not support querying formats other than images or videos'; 1070 return; 1071 } 1072 outputText = 'The supported types are:\n'; 1073 let imageFormat = await phAccessHelper.getSupportedPhotoFormats(photoAccessHelper.PhotoType.IMAGE); 1074 let result = ""; 1075 for (let i = 0; i < imageFormat.length; i++) { 1076 result += imageFormat[i]; 1077 if (i !== imageFormat.length - 1) { 1078 result += ', '; 1079 } 1080 } 1081 outputText += result; 1082 console.info('getSupportedPhotoFormats success, data is ' + outputText); 1083 } catch (error) { 1084 console.error('getSupportedPhotoFormats failed, errCode is', error); 1085 } 1086} 1087``` 1088 1089## on('photoChange')<sup>20+</sup> 1090 1091on(type: 'photoChange', callback: Callback<PhotoAssetChangeInfos>): void 1092 1093注册'photoChange'监听媒体资产,并通过callback方式返回资产变化结果,可以注册多个callback。 1094 1095**需要权限**:ohos.permission.READ_IMAGEVIDEO 1096 1097**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1098 1099**参数:** 1100 1101| 参数名 | 类型 | 必填 | 说明 | 1102|-----------|-------------------------|-----------|-----------------| 1103| type | string | 是 | 注册监听媒体资产,取值为'photoChange'。注册完成后,有资产发生变化时,通过callback返回变更信息。 | 1104| callback | Callback<[PhotoAssetChangeInfos](arkts-apis-photoAccessHelper-i.md#photoassetchangeinfos20)> | 是 | 返回变更的媒体资产信息[PhotoAssetChangeInfos](arkts-apis-photoAccessHelper-i.md#photoassetchangeinfos20)。<br>**注意:** 该接口可以注册多个不同的callback监听,[off('photoChange')](#offphotochange20)即可以关闭所有监听,也可以关闭指定callback监听。 | 1105 1106**错误码:** 1107 1108以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[媒体库错误码](errcode-medialibrary.md)。 1109 1110| 错误码ID | 错误信息 | 1111| -------- | ---------------------------------------- | 1112| 201 | Permission denied. | 1113| 23800151 | The scenario parameter verification fails.<br>Possible causes: 1. The type is not fixed at 'photoChange'; 2. The same callback is registered repeatedly. | 1114| 23800301 | Internal system error. You are advised to retry and check the logs.<br>Possible causes: 1. The database is corrupted. 2. The file system is abnormal. 3. The IPC request timed out. | 1115 1116**示例:** 1117 1118phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1119 1120```ts 1121import { dataSharePredicates } from '@kit.ArkData' 1122 1123let onCallback1 = (changeData: photoAccessHelper.PhotoAssetChangeInfos) => { 1124 console.info('onCallback1 success, changData: ' + JSON.stringify(changeData)); 1125 // file had changed, do something. 1126} 1127let onCallback2 = (changeData: photoAccessHelper.PhotoAssetChangeInfos) => { 1128 console.info('onCallback2 success, changData: ' + JSON.stringify(changeData)); 1129 // file had changed, do something. 1130} 1131 1132async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context){ 1133 console.info('onPhotoChangeDemo.'); 1134 1135 try { 1136 // 注册onCallback1监听。 1137 phAccessHelper.on('photoChange', onCallback1); 1138 // 注册onCallback2监听。 1139 phAccessHelper.on('photoChange', onCallback2); 1140 } catch (error) { 1141 console.error('onPhotoChangeDemo failed, errCode is', error); 1142 } 1143} 1144``` 1145 1146## off('photoChange')<sup>20+</sup> 1147 1148off(type: 'photoChange', callback?: Callback<PhotoAssetChangeInfos>): void 1149 1150取消对'photoChange'媒体资产的监听。存在多个callback监听时,可以取消指定注册的callback监听;不指定callback时取消所有监听。 1151 1152**需要权限**:ohos.permission.READ_IMAGEVIDEO 1153 1154**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1155 1156**参数:** 1157 1158| 参数名 | 类型 | 必填 | 说明 | 1159|-----------|-------------------------|-----------|-----------------| 1160| type | string | 是 | 取消监听媒体资产,取值为'photoChange'。取消监听后,有资产发生变化时,不再通过callback返回变更信息。 | 1161| callback | Callback<[PhotoAssetChangeInfos](arkts-apis-photoAccessHelper-i.md#photoassetchangeinfos20)> | 否 | 取消[on('photoChange')](#onphotochange20)注册时指定的callback监听;不填时,则取消对'photoChange'的所有监听。<br>**注意:** 取消注册的callback后,有资产发生变化时,不会进入此回调。 | 1162 1163**错误码:** 1164 1165以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[媒体库错误码](errcode-medialibrary.md)。 1166 1167| 错误码ID | 错误信息 | 1168| -------- | ---------------------------------------- | 1169| 201 | Permission denied. | 1170| 23800151 | The scenario parameter verification fails.<br>Possible causes: 1. The type is not fixed at 'photoChange'; 2. The same callback is unregistered repeatedly. | 1171| 23800301 | Internal system error. You are advised to retry and check the logs.<br>Possible causes: 1. The database is corrupted. 2. The file system is abnormal. 3. The IPC request timed out. | 1172 1173**示例:** 1174 1175phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1176 1177```ts 1178import { dataSharePredicates } from '@kit.ArkData' 1179 1180let onCallback1 = (changeData: photoAccessHelper.PhotoAssetChangeInfos) => { 1181 console.info('onCallback1 success, changData: ' + JSON.stringify(changeData)); 1182 // file had changed, do something. 1183} 1184let onCallback2 = (changeData: photoAccessHelper.PhotoAssetChangeInfos) => { 1185 console.info('onCallback2 success, changData: ' + JSON.stringify(changeData)); 1186 // file had changed, do something. 1187} 1188 1189async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context){ 1190 console.info('offPhotoChangeDemo.'); 1191 1192 try { 1193 // 注册onCallback1监听。 1194 phAccessHelper.on('photoChange', onCallback1); 1195 // 注册onCallback2监听。 1196 phAccessHelper.on('photoChange', onCallback2); 1197 1198 // 关闭onCallback1监听,onCallback2继续监听。 1199 phAccessHelper.off('photoChange', onCallback1); 1200 } catch (error) { 1201 console.error('offPhotoChangeDemo failed, errCode is', error); 1202 } 1203} 1204``` 1205 1206## on('photoAlbumChange')<sup>20+</sup> 1207 1208on(type: 'photoAlbumChange', callback: Callback<AlbumChangeInfos>): void 1209 1210注册'photoAlbumChange'监听相册,并通过callback方式返回相册变化结果,可以注册多个callback。 1211 1212**需要权限**:ohos.permission.READ_IMAGEVIDEO 1213 1214**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1215 1216**参数:** 1217 1218| 参数名 | 类型 | 必填 | 说明 | 1219|-----------|-------------------------|-----------|-----------------| 1220| type | string | 是 | 注册监听相册,取值为'photoAlbumChange'。注册完成后,有相册发生变化时,通过callback返回变更信息。 | 1221| callback | Callback<[AlbumChangeInfos](arkts-apis-photoAccessHelper-i.md#albumchangeinfos20)> | 是 | 返回变更的相册信息[AlbumChangeInfos](arkts-apis-photoAccessHelper-i.md#albumchangeinfos20)。<br>**注意:** 该接口可以注册多个不同的callback监听,[off('photoAlbumChange')](#offphotoalbumchange20)即可以关闭所有监听,也可以关闭指定callback监听。 | 1222 1223**错误码:** 1224 1225以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[媒体库错误码](errcode-medialibrary.md)。 1226 1227| 错误码ID | 错误信息 | 1228| -------- | ---------------------------------------- | 1229| 201 | Permission denied. | 1230| 23800151 | The scenario parameter verification fails.<br>Possible causes: 1. The type is not fixed at 'photoAlbumChange'; 2. The same callback is registered repeatedly. | 1231| 23800301 | Internal system error. You are advised to retry and check the logs.<br>Possible causes: 1. The database is corrupted. 2. The file system is abnormal. 3. The IPC request timed out. | 1232 1233**示例:** 1234 1235phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1236 1237```ts 1238import { dataSharePredicates } from '@kit.ArkData' 1239 1240let onCallback1 = (changeData: photoAccessHelper.AlbumChangeInfos) => { 1241 console.info('onCallback1 success, changData: ' + JSON.stringify(changeData)); 1242 // file had changed, do something. 1243} 1244let onCallback2 = (changeData: photoAccessHelper.AlbumChangeInfos) => { 1245 console.info('onCallback2 success, changData: ' + JSON.stringify(changeData)); 1246 // file had changed, do something. 1247} 1248 1249async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context){ 1250 console.info('onPhotoAlbumChangeDemo.'); 1251 1252 try { 1253 // 注册onCallback1监听。 1254 phAccessHelper.on('photoAlbumChange', onCallback1); 1255 // 注册onCallback2监听。 1256 phAccessHelper.on('photoAlbumChange', onCallback2); 1257 } catch (error) { 1258 console.error('onPhotoAlbumChangeDemo failed, errCode is', error); 1259 } 1260} 1261``` 1262 1263## off('photoAlbumChange')<sup>20+</sup> 1264 1265off(type: 'photoAlbumChange', callback?: Callback<AlbumChangeInfos>): void 1266 1267取消对'photoAlbumChange'相册的监听。存在多个callback监听时,可以取消指定注册的callback监听;不指定callback时取消所有监听。 1268 1269**需要权限**:ohos.permission.READ_IMAGEVIDEO 1270 1271**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1272 1273**参数:** 1274 1275| 参数名 | 类型 | 必填 | 说明 | 1276|-----------|-------------------------|-----------|-----------------| 1277| type | string | 是 | 取消监听相册,取值为'photoAlbumChange'。取消监听后,有相册发生变化时,不再通过callback返回变更信息。 | 1278| callback | Callback<[AlbumChangeInfos](arkts-apis-photoAccessHelper-i.md#albumchangeinfos20)> | 否 | 取消[on('photoAlbumChange')](#onphotoalbumchange20)注册时指定的callback监听;不填时,则取消对'photoAlbumChange'的所有监听。<br>**注意:** 取消注册的callback后,有相册发生变化时,不会进入此回调。 | 1279 1280**错误码:** 1281 1282以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[媒体库错误码](errcode-medialibrary.md)。 1283 1284| 错误码ID | 错误信息 | 1285| -------- | ---------------------------------------- | 1286| 201 | Permission denied. | 1287| 23800151 | The scenario parameter verification fails.<br>Possible causes: 1. The type is not fixed at 'photoAlbumChange'; 2. The same callback is unregistered repeatedly. | 1288| 23800301 | Internal system error. You are advised to retry and check the logs.<br>Possible causes: 1. The database is corrupted. 2. The file system is abnormal. 3. The IPC request timed out. | 1289 1290**示例:** 1291 1292phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1293 1294```ts 1295import { dataSharePredicates } from '@kit.ArkData' 1296 1297let onCallback1 = (changeData: photoAccessHelper.AlbumChangeInfos) => { 1298 console.info('onCallback1 success, changData: ' + JSON.stringify(changeData)); 1299 // file had changed, do something. 1300} 1301let onCallback2 = (changeData: photoAccessHelper.AlbumChangeInfos) => { 1302 console.info('onCallback2 success, changData: ' + JSON.stringify(changeData)); 1303 // file had changed, do something. 1304} 1305 1306async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper, context: Context){ 1307 console.info('onPhotoAlbumChangeDemo.'); 1308 1309 try { 1310 // 注册onCallback1监听。 1311 phAccessHelper.on('photoAlbumChange', onCallback1); 1312 // 注册onCallback2监听。 1313 phAccessHelper.on('photoAlbumChange', onCallback2); 1314 1315 // 关闭onCallback1监听,onCallback2继续监听。 1316 phAccessHelper.off('photoAlbumChange', onCallback1); 1317 } catch (error) { 1318 console.error('onPhotoAlbumChangeDemo failed, errCode is', error); 1319 } 1320} 1321``` 1322 1323## getPhotoPickerComponentDefaultAlbumName<sup>20+</sup> 1324 1325getPhotoPickerComponentDefaultAlbumName(): Promise<string> 1326 1327应用使用PhotoPickerComponent组件选择照片时,支持调用API获取组件默认显示相册的相册名字符串。跟随当前系统语言,支持返回当前语言的相册名。使用Promise异步回调。 1328 1329**原子化服务API**: 从API version 20开始,该接口支持在原子化服务中使用。 1330 1331**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1332 1333**返回值:** 1334 1335| 类型 | 说明 | 1336| --------------------------------------- | ----------------- | 1337| Promise<string>| Promise对象,返回默认相册的相册名。 | 1338 1339**错误码:** 1340 1341以下错误码的详细介绍请参见[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1342 1343| 错误码ID | 错误信息 | 1344| -------- | ---------------------------------------- | 1345| 23800301 | Internal system error. It is recommended to retry and check the logs. Possible causes: 1. The IPC request timed out. 2. system running error. | 1346 1347**示例:** 1348 1349```ts 1350import { BusinessError } from '@kit.BasicServicesKit'; 1351import {photoAccessHelper} from '@kit.MediaLibraryKit'; 1352 1353async function example(context: Context) { 1354 console.info('getPhotoPickerComponentDefaultAlbumNameDemo'); 1355 let phAccessHelper: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 1356 1357 phAccessHelper.getPhotoPickerComponentDefaultAlbumName().then((defaultAlbumName) => { 1358 console.info('getPhotoPickerComponentDefaultAlbumName success, defaultAlbumName is ' + defaultAlbumName); 1359 }).catch((err: BusinessError) => { 1360 console.error(`getPhotoPickerComponentDefaultAlbumName failed with error: ${err.code}, ${err.message}`); 1361 }); 1362} 1363``` 1364 1365## createDeleteRequest<sup>(deprecated)</sup> 1366 1367createDeleteRequest(uriList: Array<string>, callback: AsyncCallback<void>): void 1368 1369创建一个弹出框来删除照片,删除的文件进入到回收站,使用callback方式返回结果。 1370 1371> **说明:** 1372> 1373> 从API version 10开始支持,从API version 11开始废弃。建议使用[MediaAssetChangeRequest.deleteAssets](arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#deleteassets11-1)替代。 1374 1375**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 1376 1377**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1378 1379**参数:** 1380 1381| 参数名 | 类型 | 必填 | 说明 | 1382| -------- | ------------------------- | ---- | ---------- | 1383| uriList | Array<string> | 是 | 待删除的媒体文件uri数组,最大删除数量300。 | 1384| callback | AsyncCallback<void> | 是 | callback返回void。 | 1385 1386**错误码:** 1387 1388接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1389 1390错误码13900012,请参考[开发准备](../../media/medialibrary/photoAccessHelper-preparation.md)。 1391 1392| 错误码ID | 错误信息 | 1393| -------- | ---------------------------------------- | 1394| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1395| 13900012 | Permission denied. | 1396| 13900020 | Invalid argument. | 1397| 14000011 | System inner fail. | 1398 1399**示例:** 1400 1401phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1402 1403```ts 1404import { dataSharePredicates } from '@kit.ArkData'; 1405 1406async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 1407 console.info('createDeleteRequestDemo'); 1408 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1409 let fetchOptions: photoAccessHelper.FetchOptions = { 1410 fetchColumns: [], 1411 predicates: predicates 1412 }; 1413 try { 1414 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1415 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1416 if (asset === undefined) { 1417 console.error('asset not exist'); 1418 return; 1419 } 1420 phAccessHelper.createDeleteRequest([asset.uri], (err) => { 1421 if (err === undefined) { 1422 console.info('createDeleteRequest successfully'); 1423 } else { 1424 console.error(`createDeleteRequest failed with error: ${err.code}, ${err.message}`); 1425 } 1426 }); 1427 } catch (err) { 1428 console.error(`fetch failed, error: ${err.code}, ${err.message}`); 1429 } 1430} 1431``` 1432 1433## createDeleteRequest<sup>(deprecated)</sup> 1434 1435createDeleteRequest(uriList: Array<string>): Promise<void> 1436 1437创建一个弹出框来删除照片,删除的文件进入到回收站,使用Promise方式返回结果。 1438 1439> **说明:** 1440> 1441> 从API version 10开始支持,从API version 11开始废弃。建议使用[MediaAssetChangeRequest.deleteAssets](arkts-apis-photoAccessHelper-MediaAssetChangeRequest.md#deleteassets11-1)替代。 1442 1443**需要权限**:ohos.permission.WRITE_IMAGEVIDEO 1444 1445**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1446 1447**参数:** 1448 1449| 参数名 | 类型 | 必填 | 说明 | 1450| -------- | ------------------------- | ---- | ---------- | 1451| uriList | Array<string> | 是 | 待删除的媒体文件uri数组,最大删除数量300。 | 1452 1453**返回值:** 1454 1455| 类型 | 说明 | 1456| --------------------------------------- | ----------------- | 1457| Promise<void>| Promise对象,返回void。 | 1458 1459**错误码:** 1460 1461接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 1462 1463错误码13900012,请参考[开发准备](../../media/medialibrary/photoAccessHelper-preparation.md)。 1464 1465| 错误码ID | 错误信息 | 1466| -------- | ---------------------------------------- | 1467| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 1468| 13900012 | Permission denied. | 1469| 13900020 | Invalid argument. | 1470| 14000011 | System inner fail. | 1471 1472**示例:** 1473 1474phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 1475 1476```ts 1477import { dataSharePredicates } from '@kit.ArkData'; 1478 1479async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 1480 console.info('createDeleteRequestDemo'); 1481 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 1482 let fetchOptions: photoAccessHelper.FetchOptions = { 1483 fetchColumns: [], 1484 predicates: predicates 1485 }; 1486 try { 1487 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOptions); 1488 let asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); 1489 if (asset === undefined) { 1490 console.error('asset not exist'); 1491 return; 1492 } 1493 await phAccessHelper.createDeleteRequest([asset.uri]); 1494 console.info('createDeleteRequest successfully'); 1495 } catch (err) { 1496 console.error(`createDeleteRequest failed with error: ${err.code}, ${err.message}`); 1497 } 1498} 1499``` 1500 1501## getRecentPhotoInfo<sup>20+</sup> 1502 1503getRecentPhotoInfo(options?: RecentPhotoOptions): Promise\<RecentPhotoInfo> 1504 1505应用使用RecentPhotoComponent组件查看最近图片时,支持调用API获取最近图片信息。使用Promise异步回调。 1506 1507**原子化服务API**: 从API version 20开始,该接口支持在原子化服务中使用。 1508 1509**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 1510 1511**参数:** 1512 1513| 参数名 | 类型 | 必填 | 说明 | 1514| ------- | ------- | ---- | -------------------------- | 1515| options | [RecentPhotoOptions](arkts-apis-photoAccessHelper-class.md#recentphotooptions20) | 否 | 最近图片配置选项参数。若无此参数,则按照时间找到最近图片。<br>该参数在配置的情况下,需与RecentPhotoComponent组件中的options配置相同才可以查到一样的图片,否则可能存在接口能查到最近图片,组件没查到最近图片的情况。 | 1516 1517**返回值:** 1518 1519| 类型 | 说明 | 1520| --------------------------------------- | ----------------- | 1521| Promise\<[RecentPhotoInfo](arkts-apis-photoAccessHelper-class.md#recentphotoinfo20)>| Promise对象,返回最近图片信息。 | 1522 1523**示例:** 1524 1525```ts 1526import { BusinessError } from '@kit.BasicServicesKit'; 1527import { photoAccessHelper, PhotoSource, RecentPhotoOptions} from '@kit.MediaLibraryKit'; 1528 1529async function example(context: Context) { 1530 console.info('getRecentPhotoInfoDemo'); 1531 let phAccessHelper: photoAccessHelper.PhotoAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); 1532 let recentPhotoOptions: RecentPhotoOptions = { 1533 period: 60 * 60, 1534 MIMEType: photoAccessHelper.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE, 1535 photoSource: PhotoSource.ALL 1536 } 1537 1538 phAccessHelper.getRecentPhotoInfo(recentPhotoOptions).then((recentPhotoInfo) => { 1539 console.info('getRecentPhotoInfo success, recentPhotoInfo is ' + JSON.stringify(recentPhotoInfo)); 1540 }).catch((err: BusinessError) => { 1541 console.error(`getRecentPhotoInfo failed with error: ${err.code}, ${err.message}`); 1542 }); 1543} 1544``` 1545