# @ohos.file.sendablePhotoAccessHelper (基于Sendable对象的相册管理模块) 该模块基于Sendable对象,提供相册管理模块能力,包括创建相册以及访问、修改相册中的媒体数据信息等。 > **说明:** > > 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 ## 导入模块 ```ts import { sendablePhotoAccessHelper } from '@kit.MediaLibraryKit'; ``` ## sendablePhotoAccessHelper.getPhotoAccessHelper getPhotoAccessHelper(context: Context): PhotoAccessHelper 获取相册管理模块的实例,用于访问和修改相册中的媒体文件。 **模型约束**: 此接口仅可在Stage模型下使用。 **原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | ------------------------------------------------------------ | ---- | -------------------------- | | context | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | 是 | 传入Ability实例的Context。 | **返回值:** | 类型 | 说明 | | --------------------------------------- | :------------------- | | [PhotoAccessHelper](#photoaccesshelper) | 相册管理模块的实例。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **示例:** ```ts //此处获取的phAccessHelper实例为全局对象,后续使用到phAccessHelper的地方默认为使用此处获取的对象,如未添加此段代码报phAccessHelper未定义的错误请自行添加 let context = getContext(this); let phAccessHelper = sendablePhotoAccessHelper.getPhotoAccessHelper(context); ``` ## PhotoAccessHelper ### getAssets getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 获取图片和视频资源,使用Promise方式返回结果。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **需要权限**:ohos.permission.READ_IMAGEVIDEO 对于未申请'ohos.permission.READ_IMAGEVIDEO'权限的应用,可以通过picker的方式调用该接口来查询指定uri对应的图片或视频资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-photoviewpicker.md#指定uri获取图片或视频资源)。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | --------------------------------------------------------- | ---- | -------------------- | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 图片和视频检索选项。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | --------------------------------------- | | Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise对象,返回图片和视频数据结果集。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getAssets'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; try { let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOptions); if (fetchResult !== undefined) { console.info('fetchResult success'); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); if (photoAsset !== undefined) { console.info('photoAsset.displayName :' + photoAsset.displayName); } } } catch (err) { console.error(`getAssets failed, error: ${err.code}, ${err.message}`); } } ``` ### getBurstAssets getBurstAssets(burstKey: string, options: FetchOptions): Promise<FetchResult<PhotoAsset>> 获取连拍照片资源,使用Promise方式返回结果。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **需要权限**:ohos.permission.READ_IMAGEVIDEO **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | | burstKey | string | 是 | 一组连拍照片的唯一标识:uuid(可传入[PhotoKeys](js-apis-photoAccessHelper.md#photokeys)的BURST_KEY) | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 连拍照片检索选项。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | ------------------------------------- | | Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise对象,返回连拍照片数据结果集。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 201 | Permission denied. | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 14000011 | Internal system error. | **示例:** ```ts import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { dataSharePredicates } from '@kit.ArkData'; async function example() { console.info('getBurstAssets'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAssetList: Array = await fetchResult.getAllObjects(); let photoAsset: sendablePhotoAccessHelper.PhotoAsset; // burstKey为36位的uuid,可以根据photoAccessHelper.PhotoKeys获取。 for(photoAsset of photoAssetList){ let burstKey: string = photoAccessHelper.PhotoKeys.BURST_KEY.toString(); let photoAccessBurstKey: photoAccessHelper.MemberType = photoAsset.get(burstKey).toString(); try { let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getBurstAssets(photoAccessBurstKey, fetchOption); if (fetchResult !== undefined) { console.info('fetchResult success'); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); if (photoAsset !== undefined) { console.info('photoAsset.displayName :' + photoAsset.displayName); } } } catch (err) { console.error(`getBurstAssets failed, error: ${err.code}, ${err.message}`); } } } ``` ### createAsset createAsset(photoType: PhotoType, extension: string, options?: CreateOptions): Promise<string> 指定待创建的文件类型、后缀和创建选项,创建图片或视频资源,使用Promise方式返回结果。 此接口在未申请相册管理模块权限'ohos.permission.WRITE_IMAGEVIDEO'时,可以使用安全控件创建媒体资源,详情请参考[开发指南](../../media/medialibrary/photoAccessHelper-savebutton.md)。 **原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **需要权限**:ohos.permission.WRITE_IMAGEVIDEO **参数:** | 参数名 | 类型 | 必填 | 说明 | | --------- | ----------------------------------------------------------- | ---- | ------------------------------------ | | photoType | [PhotoType](#phototype) | 是 | 创建的文件类型,IMAGE或者VIDEO类型。 | | extension | string | 是 | 文件名后缀参数,例如:'jpg'。 | | options | [CreateOptions](js-apis-photoAccessHelper.md#createoptions) | 否 | 创建选项,例如{title: 'testPhoto'}。 | **返回值:** | 类型 | 说明 | | --------------------- | ---------------------------------------- | | Promise<string> | Promise对象,返回创建的图片和视频的uri。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('createAssetDemo'); try { let photoType: sendablePhotoAccessHelper.PhotoType = sendablePhotoAccessHelper.PhotoType.IMAGE; let extension: string = 'jpg'; let options: photoAccessHelper.CreateOptions = { title: 'testPhoto' } let uri: string = await phAccessHelper.createAsset(photoType, extension, options); console.info('createAsset uri' + uri); console.info('createAsset successfully'); } catch (err) { console.error(`createAsset failed, error: ${err.code}, ${err.message}`); } } ``` ### getAlbums getAlbums(type: AlbumType, subtype: AlbumSubtype, options?: FetchOptions): Promise<FetchResult<Album>> 根据检索选项和相册类型获取相册,使用Promise方式返回结果。 获取相册前需先保证相册存在。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **需要权限**:ohos.permission.READ_IMAGEVIDEO **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | --------------------------------------------------------- | ---- | -------------------------------------- | | type | [AlbumType](#albumtype) | 是 | 相册类型。 | | subtype | [AlbumSubtype](#albumsubtype) | 是 | 相册子类型。 | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 否 | 检索选项,不填时默认根据相册类型检索。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | ----------------------------------- | | Promise<[FetchResult](#fetchresult)<[Album](#album)>> | Promise对象,返回获取相册的结果集。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { // 示例代码中为获取相册名为newAlbumName的相册。 console.info('getAlbumsDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); predicates.equalTo('album_name', 'newAlbumName'); let fetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions).then( async (fetchResult) => { if (fetchResult === undefined) { console.error('getAlbumsPromise fetchResult is undefined'); return; } let album: sendablePhotoAccessHelper.Album = await fetchResult.getFirstObject(); console.info('getAlbumsPromise successfully, albumName: ' + album.albumName); fetchResult.close(); }).catch((err: BusinessError) => { console.error(`getAlbumsPromise failed with err: ${err.code}, ${err.message}`); }); } ``` ### getAlbums getAlbums(options: FetchOptions): Promise<FetchResult<Album>> 根据检索选项获取相册,使用Promise方式返回结果。 获取相册前需先保证相册存在。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **需要权限**:ohos.permission.READ_IMAGEVIDEO **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | --------------------------------------------------------- | ---- | -------- | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 检索选项 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | ----------------------------------- | | Promise<[FetchResult](#fetchresult)<[Album](#album)>> | Promise对象,返回获取相册的结果集。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { // 示例代码中为获取相册名为newAlbumName的相册。 console.info('getAlbumsDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); predicates.equalTo('album_name', 'newAlbumName'); let fetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; phAccessHelper.getAlbums(fetchOptions).then( async (fetchResult) => { if (fetchResult === undefined) { console.error('getAlbumsPromise fetchResult is undefined'); return; } let album: sendablePhotoAccessHelper.Album = await fetchResult.getFirstObject(); console.info('getAlbumsPromise successfully, albumName: ' + album.albumName); fetchResult.close(); }).catch((err: BusinessError) => { console.error(`getAlbumsPromise failed with err: ${err.code}, ${err.message}`); }); } ``` ### release release(): Promise<void> 释放PhotoAccessHelper实例,使用Promise方式返回结果。 当后续不需要使用PhotoAccessHelper 实例中的方法时调用。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ------------------- | ----------------------- | | Promise<void> | Promise对象,返回void。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts async function example() { console.info('releaseDemo'); try { console.info('use function...'); } catch (err) { console.error(`function error ...`); }finally{ try{ phAccessHelper?.release(); console.info(`release success`); } catch(e){ console.error(`release error :${e}`); } } } ``` ## PhotoAsset 提供封装文件属性的方法。 ### 属性 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core | 名称 | 类型 | 只读 | 可选 | 说明 | | ----------- | ----------------------- | ---- | ---- | ------------------------------------------------------------ | | uri | string | 是 | 否 | 媒体文件资源uri(如:file://media/Photo/1/IMG_datetime_0001/displayName.jpg),详情参见用户文件uri介绍中的[媒体文件uri](../../file-management/user-file-uri-intro.md#媒体文件uri)。 | | photoType | [PhotoType](#phototype) | 是 | 否 | 媒体文件类型 | | displayName | string | 是 | 否 | 显示文件名,包含后缀名。 | ### convertToPhotoAsset convertToPhotoAsset(): photoAccessHelper.PhotoAsset 将Sendable类型PhotoAsset转换为非Sendable类型PhotoAsset。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ---------------------------- | ------------------------------------------------------------ | | photoAccessHelper.PhotoAsset | 返回非Sendable类型的[PhotoAsset](js-apis-photoAccessHelper.md#photoasset) | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 13900020 | Invalid argument. | | 14000014 | Member is not a valid PhotoKey. | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('convertToPhotoAssetDemo'); try { let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: ['title'], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let sendablePhotoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let photoAsset: photoAccessHelper.PhotoAsset = sendablePhotoAsset.convertToPhotoAsset(); console.log(`get no sendable uri success : ${photoAsset.uri}`); } catch (err) { console.error(`convertToPhotoAsset failed. error: ${err.code}, ${err.message}`); } } ``` ### get get(member: string): MemberType 获取PhotoAsset成员参数。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------ | ---- | ------------------------------------------------------------ | | member | string | 是 | 成员参数名称,在get时,除了'uri'、'media_type'、'subtype'和'display_name'四个属性之外,其他的属性都需要在fetchColumns中填入需要get的[PhotoKeys](js-apis-photoAccessHelper.md#photokeys),例如:get title属性fetchColumns: ['title']。 | **返回值:** | 类型 | 说明 | | ----------------------------------------------------- | ---------------------------- | | [MemberType](js-apis-photoAccessHelper.md#membertype) | 获取PhotoAsset成员参数的值。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 13900020 | Invalid argument. | | 14000014 | Member is not a valid PhotoKey. | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('photoAssetGetDemo'); try { let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: ['title'], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let title: photoAccessHelper.PhotoKeys = photoAccessHelper.PhotoKeys.TITLE; let photoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title.toString()); console.info('photoAsset Get photoAssetTitle = ', photoAssetTitle); } catch (err) { console.error(`get failed. error: ${err.code}, ${err.message}`); } } ``` ### set set(member: string, value: string): void 设置PhotoAsset成员参数。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------ | ---- | ------------------------------------------------------------ | | member | string | 是 | 成员参数名称例如:[PhotoKeys](js-apis-photoAccessHelper.md#photokeys).TITLE。 | | value | string | 是 | 设置成员参数名称,只能修改[PhotoKeys](js-apis-photoAccessHelper.md#photokeys).TITLE的值。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 13900020 | Invalid argument. | | 14000014 | Member is not a valid PhotoKey. | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('photoAssetSetDemo'); try { let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: ['title'], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let title: string = photoAccessHelper.PhotoKeys.TITLE.toString(); photoAsset.set(title, 'newTitle'); } catch (err) { console.error(`set failed. error: ${err.code}, ${err.message}`); } } ``` ### commitModify commitModify(): Promise<void> 修改文件的元数据,使用Promise方式返回异步结果。 **需要权限**:ohos.permission.WRITE_IMAGEVIDEO **原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ------------------- | ----------------------- | | Promise<void> | Promise对象,返回void。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000001 | Invalid display name. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('commitModifyDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: ['title'], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); let title: string = photoAccessHelper.PhotoKeys.TITLE.toString(); let photoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); console.info('photoAsset get photoAssetTitle = ', photoAssetTitle); photoAsset.set(title, 'newTitle3'); try { await photoAsset.commitModify(); let newPhotoAssetTitle: photoAccessHelper.MemberType = photoAsset.get(title); console.info('photoAsset get newPhotoAssetTitle = ', newPhotoAssetTitle); } catch (err) { console.error(`commitModify failed. error: ${err.code}, ${err.message}`); } } ``` ### getThumbnail getThumbnail(size?: image.Size): Promise<image.PixelMap> 获取文件的缩略图,传入缩略图尺寸,使用Promise方式返回异步结果。 **需要权限**:ohos.permission.READ_IMAGEVIDEO **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ----------------------------------------------------- | ---- | ------------ | | size | [image.Size](../apis-image-kit/js-apis-image.md#size) | 否 | 缩略图尺寸。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | ----------------------------------- | | Promise<[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)> | Promise对象,返回缩略图的PixelMap。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { image } from '@kit.ImageKit'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getThumbnailDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let size: image.Size = { width: 720, height: 720 }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let asset = await fetchResult.getFirstObject(); console.info('asset displayName = ', asset.displayName); asset.getThumbnail(size).then((pixelMap) => { console.info('getThumbnail successful ' + pixelMap); }).catch((err: BusinessError) => { console.error(`getThumbnail fail with error: ${err.code}, ${err.message}`); }); } ``` ## FetchResult 文件检索结果集。 ### getCount getCount(): number 获取文件检索结果中的文件总数。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ------ | ------------------ | | number | 检索到的文件总数。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getCountDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let fetchCount = fetchResult.getCount(); console.info('fetchCount = ', fetchCount); } ``` ### isAfterLast isAfterLast(): boolean 检查结果集是否指向最后一行。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ------- | ----------------------------------------------------------- | | boolean | 当读到最后一条记录后,后续没有记录返回true,否则返回false。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let fetchCount = fetchResult.getCount(); console.info('count:' + fetchCount); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); if (fetchResult.isAfterLast()) { console.info('photoAsset isAfterLast displayName = ', photoAsset.displayName); } else { console.info('photoAsset not isAfterLast.'); } } ``` ### close close(): void 释放FetchResult实例并使其失效。无法调用其他方法。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('fetchResultCloseDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; try { let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); fetchResult.close(); console.info('close succeed.'); } catch (err) { console.error(`close fail. error: ${err.code}, ${err.message}`); } } ``` ### getFirstObject getFirstObject(): Promise<T> 获取文件检索结果中的第一个文件资产。此方法使用Promise方式来异步返回。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ---------------- | ------------------------------------- | | Promise<T> | Promise对象,返回结果集中第一个对象。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getFirstObjectDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getFirstObject(); console.info('photoAsset displayName: ', photoAsset.displayName); } ``` ### getNextObject getNextObject(): Promise<T> 获取文件检索结果中的下一个文件资产。此方法使用Promise方式来异步返回。 在调用此方法之前,必须使用[isAfterLast()](#isafterlast)来检查当前位置是否为最后一行。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ---------------- | ------------------------------------- | | Promise<T> | Promise对象,返回结果集中下一个对象。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getNextObjectDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); await fetchResult.getFirstObject(); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getNextObject(); console.info('photoAsset displayName: ', photoAsset.displayName); } ``` ### getLastObject getLastObject(): Promise<T> 获取文件检索结果中的最后一个文件资产。此方法使用Promise方式来返回。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ---------------- | --------------------------------------- | | Promise<T> | Promise对象,返回结果集中最后一个对象。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getLastObjectDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getLastObject(); console.info('photoAsset displayName: ', photoAsset.displayName); } ``` ### getObjectByPosition getObjectByPosition(index: number): Promise<T> 获取文件检索结果中具有指定索引的文件资产。此方法使用Promise形式返回文件Asset。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------ | ---- | ----------------------------- | | index | number | 是 | 要获取的文件的索引,从0开始。 | **返回值:** | 类型 | 说明 | | ---------------- | --------------------------------------------- | | Promise<T> | Promise对象,返回结果集中指定索引的一个对象。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getObjectByPositionDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAsset: sendablePhotoAccessHelper.PhotoAsset = await fetchResult.getObjectByPosition(0); console.info('photoAsset displayName: ', photoAsset.displayName); } ``` ### getAllObjects getAllObjects(): Promise<Array<T>> 获取文件检索结果中的所有文件资产。此方法使用Promise方式来异步返回。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ----------------------------- | ------------------------------------------- | | Promise<Array<T>> | Promise对象,返回结果集中所有文件资产数组。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('getAllObjectDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAssets(fetchOption); let photoAssetList: Array = await fetchResult.getAllObjects(); console.info('photoAssetList length: ', photoAssetList.length); } ``` ## Album 实体相册 ### 属性 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core | 名称 | 类型 | 只读 | 可选 | 说明 | | ------------ | ----------------------------- | ---------------------------- | ---- | ---------------- | | albumType | [AlbumType](#albumtype) | 是 | 否 | 相册类型。 | | albumSubtype | [AlbumSubtype](#albumsubtype) | 是 | 否 | 相册子类型。 | | albumName | string | 用户相册可写,预置相册不可写 | 否 | 相册名称。 | | albumUri | string | 是 | 否 | 相册Uri。 | | count | number | 是 | 否 | 相册中文件数量。 | | coverUri | string | 是 | 否 | 封面文件Uri。 | | imageCount | number | 是 | 是 | 相册中图片数量。 | | videoCount | number | 是 | 是 | 相册中视频数量。 | ### convertToPhotoAlbum convertToPhotoAlbum(): photoAccessHelper.Album 将Sendable类型Album转换为非Sendable类型Album。 **需要权限**:ohos.permission.READ_IMAGEVIDEO **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core | 类型 | 说明 | | ----------------------- | --------------------------------------------------------- | | photoAccessHelper.Album | 非Sendable类型[Album](js-apis-photoAccessHelper.md#album) | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('convertToPhotoAlbumDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let albumFetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let albumList: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); let sendableAlbum: sendablePhotoAccessHelper.Album = await albumList.getFirstObject(); let album: photoAccessHelper.Album = sendableAlbum.convertToPhotoAlbum(); album.getAssets(fetchOption).then((albumFetchResult) => { console.info('convertToPhotoAlbum successfully, getCount: ' + albumFetchResult.getCount()); }).catch((err: BusinessError) => { console.error(`convertToPhotoAlbum failed with error: ${err.code}, ${err.message}`); }); } ``` ### getAssets getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> 获取相册中的文件。该方法使用Promise来返回文件。 **需要权限**:ohos.permission.READ_IMAGEVIDEO **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------- | --------------------------------------------------------- | ---- | ---------- | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | 是 | 检索选项。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | --------------------------------------- | | Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise对象,返回图片和视频数据结果集。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('albumGetAssetsDemoPromise'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let albumFetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let albumList: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); let album: sendablePhotoAccessHelper.Album = await albumList.getFirstObject(); album.getAssets(fetchOption).then((albumFetchResult) => { console.info('album getAssets successfully, getCount: ' + albumFetchResult.getCount()); }).catch((err: BusinessError) => { console.error(`album getAssets failed with error: ${err.code}, ${err.message}`); }); } ``` ### commitModify commitModify(): Promise<void> 更新相册属性修改到数据库中。该方法使用Promise来返回结果。 **需要权限**:ohos.permission.WRITE_IMAGEVIDEO **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core **返回值:** | 类型 | 说明 | | ------------------- | ----------------------- | | Promise<void> | Promise对象,返回void。 | **错误码:** 接口抛出错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理错误码](../apis-core-file-kit/errorcode-filemanagement.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 201 | Permission denied. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **示例:** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { console.info('albumCommitModifyDemo'); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); let albumFetchOptions: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let albumList: sendablePhotoAccessHelper.FetchResult = await phAccessHelper.getAlbums(sendablePhotoAccessHelper.AlbumType.USER, sendablePhotoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions); let album: sendablePhotoAccessHelper.Album = await albumList.getFirstObject(); album.albumName = 'hello'; album.commitModify().then(() => { console.info('commitModify successfully'); }).catch((err: BusinessError) => { console.error(`commitModify failed with error: ${err.code}, ${err.message}`); }); } ``` ## PhotoType 枚举,媒体文件类型。 **原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core | 名称 | 值 | 说明 | | ----- | ---- | ------ | | IMAGE | 1 | 图片。 | | VIDEO | 2 | 视频。 | ## AlbumType 枚举,相册类型,表示是用户相册还是系统预置相册。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core | 名称 | 值 | 说明 | | ------ | ---- | -------------- | | USER | 0 | 用户相册。 | | SYSTEM | 1024 | 系统预置相册。 | ## AlbumSubtype 枚举,相册子类型,表示具体的相册类型。 **系统能力**:SystemCapability.FileManagement.PhotoAccessHelper.Core | 名称 | 值 | 说明 | | ------------- | ---------- | ---------- | | USER\_GENERIC | 1 | 用户相册。 | | FAVORITE | 1025 | 收藏夹。 | | VIDEO | 1026 | 视频相册。 | | IMAGE | 1031 | 图片相册。 | | ANY | 2147483647 | 任意相册。 |