# @ohos.file.sendablePhotoAccessHelper (Album Management Based on a Sendable object) The **sendablePhotoAccessHelper** module provides APIs for album management, including creating an album and accessing and modifying media data in an album, based on a sendable object. > **NOTE** > > The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ```ts import { sendablePhotoAccessHelper } from '@kit.MediaLibraryKit'; ``` ## sendablePhotoAccessHelper.getPhotoAccessHelper getPhotoAccessHelper(context: Context): PhotoAccessHelper Obtains a **PhotoAccessHelper** instance, which can be used for accessing and modifying media files in an album. **Model restriction**: This API can be used only in the stage model. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------------------------------ | ---- | -------------------------- | | context | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | Yes | Context of the ability instance.| **Return value** | Type | Description | | --------------------------------------- | :------------------- | | [PhotoAccessHelper](#photoaccesshelper) | **PhotoAccessHelper** instance obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | **Example** ```ts // The phAccessHelper instance obtained is a global object. It is used by default in subsequent operations. If the code snippet is not added, an error will be reported indicating that phAccessHelper is not defined. let context = getContext(this); let phAccessHelper = sendablePhotoAccessHelper.getPhotoAccessHelper(context); ``` ## PhotoAccessHelper ### getAssets getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>> Obtains media assets. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Required permissions**: ohos.permission.READ_IMAGEVIDEO If the caller does not have the ohos.permission.READ_IMAGEVIDEO permission, use Picker to access the file and then call this API based on the URI obtained by Picker. For details, see [Obtaining an Image or Video by URI](../../media/medialibrary/photoAccessHelper-photoviewpicker.md#obtaining-an-image-or-video-by-uri). **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------------------------- | ---- | -------------------- | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | Yes | Options for fetching the media assets.| **Return value** | Type | Description | | ------------------------------------------------------------ | --------------------------------------- | | Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise used to return the media assets obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```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>> Obtains burst assets. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Required permissions**: ohos.permission.READ_IMAGEVIDEO **Parameters** | Name | Type | Mandatory| Description | | -------- | --------------------------------------------------------- | ---- | ------------------------------------------------------------ | | burstKey | string | Yes | Universally Unique Identifier (UUID) of a group of burst photos, that is, **BURST_KEY** of [PhotoKeys](js-apis-photoAccessHelper.md#photokeys).| | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | Yes | Options for fetching the burst photos. | **Return value** | Type | Description | | ------------------------------------------------------------ | ------------------------------------- | | Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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. | **Example** ```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 is a 36-bit UUID, which can be obtained from 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> Creates a media asset with the specified file type, file name extension, and options. This API uses a promise to return the result. If the caller does not have the ohos.permission.WRITE_IMAGEVIDEO permission, you can create a media asset by using a security component. For details, see [Creating a Media Asset Using a Security Component](../../media/medialibrary/photoAccessHelper-savebutton.md). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Required permissions**: ohos.permission.WRITE_IMAGEVIDEO **Parameters** | Name | Type | Mandatory| Description | | --------- | ----------------------------------------------------------- | ---- | ------------------------------------ | | photoType | [PhotoType](#phototype) | Yes | Type of the file to create, which can be **IMAGE** or **VIDEO**.| | extension | string | Yes | File name extension, for example, **'jpg'**. | | options | [CreateOptions](js-apis-photoAccessHelper.md#createoptions) | No | Options for creating the media asset, for example, **{title: 'testPhoto'}**.| **Return value** | Type | Description | | --------------------- | ---------------------------------------- | | Promise<string> | Promise used to return the URI of the created media asset.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```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>> Obtains albums based on the specified options and album type. This API uses a promise to return the result. Before the operation, ensure that the albums to obtain exist. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Required permissions**: ohos.permission.READ_IMAGEVIDEO **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------------------------- | ---- | -------------------------------------- | | type | [AlbumType](#albumtype) | Yes | Type of the albums to obtain. | | subtype | [AlbumSubtype](#albumsubtype) | Yes | Subtype of the album. | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | No | Options for fetching the albums. If this parameter is not specified, the albums are obtained based on the album type by default.| **Return value** | Type | Description | | ------------------------------------------------------------ | ----------------------------------- | | Promise<[FetchResult](#fetchresult)<[Album](#album)>> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { // Obtain the album named 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>> Obtains albums. This API uses a promise to return the result. Before the operation, ensure that the albums to obtain exist. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Required permissions**: ohos.permission.READ_IMAGEVIDEO **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------------------------- | ---- | -------- | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | Yes | Options for obtaining the albums.| **Return value** | Type | Description | | ------------------------------------------------------------ | ----------------------------------- | | Promise<[FetchResult](#fetchresult)<[Album](#album)>> | Promise used to return the result.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```ts import { dataSharePredicates } from '@kit.ArkData'; import { BusinessError } from '@kit.BasicServicesKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; async function example() { // Obtain the album named 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> Releases this **PhotoAccessHelper** instance. This API uses a promise to return the result. Call this API when the APIs of the **PhotoAccessHelper** instance are no longer used. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ------------------- | ----------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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 Provides APIs for encapsulating file asset attributes. ### Properties **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core | Name | Type | Read-Only| Optional| Description | | ----------- | ----------------------- | ---- | ---- | ------------------------------------------------------------ | | uri | string | Yes | No | Media asset URI, for example, **file://media/Photo/1/IMG_datetime_0001/displayName.jpg**. For details, see [Media File URI](../../file-management/user-file-uri-intro.md#media-file-uri).| | photoType | [PhotoType](#phototype) | Yes | No | Type of the file. | | displayName | string | Yes | No | File name, including the file name extension, to display. | ### convertToPhotoAsset convertToPhotoAsset(): photoAccessHelper.PhotoAsset Converts a sendable **PhotoAsset** object to a non-sendable **PhotoAsset** object. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ---------------------------- | ------------------------------------------------------------ | | photoAccessHelper.PhotoAsset | [PhotoAsset](js-apis-photoAccessHelper.md#photoasset) object of the non-sendable type.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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. | **Example** ```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 Obtains a **PhotoAsset** member parameter. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | member | string | Yes | Name of the member parameter to obtain.
Except **'uri'**, **'media_type'**, **'subtype'**, and **'display_name'**, you must pass in [PhotoKeys](js-apis-photoAccessHelper.md#photokeys) in **fetchColumns**. For example, to obtain the title, pass in **fetchColumns: ['title']**.| **Return value** | Type | Description | | ----------------------------------------------------- | ---------------------------- | | [MemberType](js-apis-photoAccessHelper.md#membertype) | **PhotoAsset** member parameter obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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. | **Example** ```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 Sets a **PhotoAsset** member parameter. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ------------------------------------------------------------ | | member | string | Yes | Name of the parameter to set, for example, [PhotoKeys](js-apis-photoAccessHelper.md#photokeys).TITLE.| | value | string | Yes | Value to set. Only the value of [PhotoKeys](js-apis-photoAccessHelper.md#photokeys).TITLE can be changed.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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. | **Example** ```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> Commits the modification on the file metadata to the database. This API uses a promise to return the result. **Required permissions**: ohos.permission.WRITE_IMAGEVIDEO **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ------------------- | ----------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```ts import { dataSharePredicates } from '@kit.ArkData'; 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> Obtains the file thumbnail of the given size. This API uses a promise to return the result. **Required permissions**: ohos.permission.READ_IMAGEVIDEO **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ----------------------------------------------------- | ---- | ------------ | | size | [image.Size](../apis-image-kit/js-apis-image.md#size) | No | Size of the thumbnail.| **Return value** | Type | Description | | ------------------------------------------------------------ | ----------------------------------- | | Promise<[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)> | Promise used to return the PixelMap of the thumbnail.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```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 Provides APIs to manage the file retrieval result. ### getCount getCount(): number Obtains the total number of files in the result set. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ------ | ------------------ | | number | Total number of files obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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 Checks whether the cursor is in the last row of the result set. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ------- | ----------------------------------------------------------- | | boolean | Returns **true** if the cursor is in the last row of the result set; returns **false** otherwise.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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 Closes this **FetchFileResult** instance to invalidate it. After this instance is closed, the APIs in this instance cannot be invoked. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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> Obtains the first asset in the result set. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ---------------- | ------------------------------------- | | Promise<T> | Promise used to return the first object in the result set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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> Obtains the next asset in the result set. This API uses a promise to return the result. Before using this API, you must use [isAfterLast()](#isafterlast) to check whether the current position is the end of the result set. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ---------------- | ------------------------------------- | | Promise<T> | Promise used to return the next object in the result set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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> Obtains the last asset in the result set. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ---------------- | --------------------------------------- | | Promise<T> | Promise used to return the last object in the result set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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> Obtains the asset with the given index in the result set. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Parameters** | Name| Type | Mandatory| Description | | ------ | ------ | ---- | ----------------------------- | | index | number | Yes | Index of the asset to obtain. The value starts from **0**.| **Return value** | Type | Description | | ---------------- | --------------------------------------------- | | Promise<T> | Promise used to return the asset obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```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>> Obtains all the file assets in the result set. This API uses a promise to return the result. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ----------------------------- | ------------------------------------------- | | Promise<Array<T>> | Promise used to return all the assets in the result set.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | | 13900020 | Invalid argument. | | 14000011 | Internal system error | **Example** ```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 Provides APIs to manage albums. ### Properties **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core | Name | Type | Read-Only | Optional| Description | | ------------ | ----------------------------- | ---------------------------- | ---- | ---------------- | | albumType | [AlbumType](#albumtype) | Yes | No | Type of the album. | | albumSubtype | [AlbumSubtype](#albumsubtype) | Yes | No | Subtype of the album. | | albumName | string | Yes for a user album; no for a system album.| No | Name of the album. | | albumUri | string | Yes | No | URI of the album. | | count | number | Yes | No | Number of files in the album.| | coverUri | string | Yes | No | URI of the cover file of the album. | | imageCount | number | Yes | Yes | Number of images in the album.| | videoCount | number | Yes | Yes | Number of videos in the album.| ### convertToPhotoAlbum convertToPhotoAlbum(): photoAccessHelper.Album Converts this sendable album to a non-sendable album. **Required permissions**: ohos.permission.READ_IMAGEVIDEO **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ----------------------- | --------------------------------------------------------- | | photoAccessHelper.Album | Non-sendable [Album](js-apis-photoAccessHelper.md#album).| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```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>> Obtains media assets. This API uses a promise to return the result. **Required permissions**: ohos.permission.READ_IMAGEVIDEO **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Parameters** | Name | Type | Mandatory| Description | | ------- | --------------------------------------------------------- | ---- | ---------- | | options | [FetchOptions](js-apis-photoAccessHelper.md#fetchoptions) | Yes | Options for fetching the assets.| **Return value** | Type | Description | | ------------------------------------------------------------ | --------------------------------------- | | Promise<[FetchResult](#fetchresult)<[PhotoAsset](#photoasset)>> | Promise used to return the media assets obtained.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```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> Commits the modification on the album attributes to the database. This API uses a promise to return the result. **Required permissions**: ohos.permission.WRITE_IMAGEVIDEO **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core **Return value** | Type | Description | | ------------------- | ----------------------- | | Promise<void> | Promise that returns no value.| **Error codes** For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](../apis-core-file-kit/errorcode-filemanagement.md). | ID| Error Message | | -------- | ------------------------------------------------------------ | | 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 | **Example** ```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 Enumerates media file types. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core | Name | Value | Description | | ----- | ---- | ------ | | IMAGE | 1 | Image.| | VIDEO | 2 | Video.| ## AlbumType Enumerates the album types. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core | Name | Value | Description | | ------ | ---- | -------------- | | USER | 0 | User album. | | SYSTEM | 1024 | System album.| ## AlbumSubtype Enumerate the album subtypes. **System capability**: SystemCapability.FileManagement.PhotoAccessHelper.Core | Name | Value | Description | | ------------- | ---------- | ---------- | | USER\_GENERIC | 1 | User album.| | FAVORITE | 1025 | Favorites. | | VIDEO | 1026 | Video album.| | IMAGE | 1031 | Photo album.| | ANY | 2147483647 | Any album.|