1# Interface (AbsAlbum) 2<!--Kit: Media Library Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @yixiaoff--> 5<!--Designer: @liweilu1--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9> **说明:** 10> 11> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 12 13## 导入模块 14 15```ts 16import { photoAccessHelper } from '@kit.MediaLibraryKit'; 17``` 18 19## 属性 20 21**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 22 23| 名称 | 类型 | 只读 | 可选 | 说明 | 24| ------------ | ------ | ---- | ---- | ------- | 25| albumType | [AlbumType](arkts-apis-photoAccessHelper-e.md#albumtype) | 是 | 否 | 相册类型。 | 26| albumSubtype | [AlbumSubtype](arkts-apis-photoAccessHelper-e.md#albumsubtype) | 是 | 否 | 相册子类型。 | 27| albumName | string | 否 | 否 | 相册名称。预置相册不可写,用户相册可写。 | 28| albumUri | string | 是 | 否 | 相册uri。 | 29| count | number | 是 | 否 | 相册中文件数量。 | 30| coverUri | string | 是 | 否 | 封面文件uri。 | 31 32## getAssets 33 34getAssets(options: FetchOptions, callback: AsyncCallback<FetchResult<PhotoAsset>>): void 35 36获取相册中的文件。该方法使用callback形式来返回。 37 38**需要权限**:ohos.permission.READ_IMAGEVIDEO 39 40**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 41 42**参数:** 43 44| 参数名 | 类型 | 必填 | 说明 | 45| -------- | ------------------------- | ---- | ---------- | 46| options | [FetchOptions](arkts-apis-photoAccessHelper-i.md#fetchoptions) | 是 | 检索选项。 | 47| callback | AsyncCallback<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[PhotoAsset](arkts-apis-photoAccessHelper-PhotoAsset.md)>> | 是 | callback返回图片和视频数据结果集。 | 48 49**错误码:** 50 51接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 52 53在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 54 55| 错误码ID | 错误信息 | 56| -------- | ---------------------------------------- | 57| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 58| 201 | Permission denied. | 59| 13900020 | Invalid argument. | 60| 14000011 | System inner fail. | 61 62**示例:** 63 64phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 65 66```ts 67import { dataSharePredicates } from '@kit.ArkData'; 68 69async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 70 console.info('albumGetAssetsDemoCallback'); 71 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 72 let albumFetchOptions: photoAccessHelper.FetchOptions = { 73 fetchColumns: [], 74 predicates: predicates 75 }; 76 let fetchOption: photoAccessHelper.FetchOptions = { 77 fetchColumns: [], 78 predicates: predicates 79 }; 80 let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 81 let album: photoAccessHelper.Album = await albumList.getFirstObject(); 82 album.getAssets(fetchOption, (err, albumFetchResult) => { 83 if (albumFetchResult !== undefined) { 84 console.info('album getAssets successfully, getCount: ' + albumFetchResult.getCount()); 85 } else { 86 console.error(`album getAssets failed with error: ${err.code}, ${err.message}`); 87 } 88 }); 89} 90``` 91 92## getAssets 93 94getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 95 96获取相册中的文件。该方法使用Promise来返回。 97 98**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 99 100**需要权限**:ohos.permission.READ_IMAGEVIDEO 101 102**系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core 103 104**参数:** 105 106| 参数名 | 类型 | 必填 | 说明 | 107| -------- | ------------------------- | ---- | ---------- | 108| options | [FetchOptions](arkts-apis-photoAccessHelper-i.md#fetchoptions) | 是 | 检索选项。 | 109 110**返回值:** 111 112| 类型 | 说明 | 113| --------------------------------------- | ----------------- | 114| Promise<[FetchResult](arkts-apis-photoAccessHelper-FetchResult.md)<[PhotoAsset](arkts-apis-photoAccessHelper-PhotoAsset.md)>> | Promise对象,返回图片和视频数据结果集。 | 115 116**错误码:** 117 118接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 119 120在API 13及之前的版本,无相关权限返回错误码13900012;从API 14开始,无相关权限返回错误码201。 121 122| 错误码ID | 错误信息 | 123| -------- | ---------------------------------------- | 124| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 125| 201 | Permission denied. | 126| 13900020 | Invalid argument. | 127| 14000011 | System inner fail. | 128 129**示例:** 130 131phAccessHelper的创建请参考[photoAccessHelper.getPhotoAccessHelper](arkts-apis-photoAccessHelper-f.md#photoaccesshelpergetphotoaccesshelper)的示例使用。 132 133```ts 134import { dataSharePredicates } from '@kit.ArkData'; 135import { BusinessError } from '@kit.BasicServicesKit'; 136 137async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) { 138 console.info('albumGetAssetsDemoPromise'); 139 let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); 140 let albumFetchOptions: photoAccessHelper.FetchOptions = { 141 fetchColumns: [], 142 predicates: predicates 143 }; 144 let fetchOption: photoAccessHelper.FetchOptions = { 145 fetchColumns: [], 146 predicates: predicates 147 }; 148 let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); 149 let album: photoAccessHelper.Album = await albumList.getFirstObject(); 150 album.getAssets(fetchOption).then((albumFetchResult) => { 151 console.info('album getAssets successfully, getCount: ' + albumFetchResult.getCount()); 152 }).catch((err: BusinessError) => { 153 console.error(`album getAssets failed with error: ${err.code}, ${err.message}`); 154 }); 155} 156``` 157