Interface (AbsAlbum)
NOTE
The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Modules to Import
import { photoAccessHelper } from '@kit.MediaLibraryKit';
Properties
System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| albumType | AlbumType | Yes | No | Type of the album. |
| albumSubtype | AlbumSubtype | Yes | No | Subtype of the album. |
| albumName | string | No | No | Name of the album. System albums are not writable, whereas user albums can be written to. |
| 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. |
| lpath23+ | string | Yes | Yes | Virtual path of the album. |
| changeTime23+ | number | Yes | Yes | Time when the album is changed. |
getAssets
getAssets(options: FetchOptions, callback: AsyncCallback<FetchResult<PhotoAsset>>): void
Obtains image and video assets. This API uses an asynchronous callback to return the result.
Required permissions: ohos.permission.READ_IMAGEVIDEO
System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| options | FetchOptions | Yes | Retrieval options. |
| callback | AsyncCallback<FetchResult<PhotoAsset>> | Yes | Callback function. If files from the album are obtained successfully, err is undefined, and data is the result set of the obtained image and video data (FetchResult). Otherwise, err is an error object. |
Error codes
For details about the error codes, see Universal Error Codes and File Management Error Codes.
In API version 13 and earlier versions, if the caller does not have the required permission, error code 13900012 is returned. Starting from API version 14, the same situation raises error code 201.
| 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 | System inner fail. |
Example
For details about how to create a phAccessHelper instance, see the example provided in photoAccessHelper.getPhotoAccessHelper.
import { dataSharePredicates } from '@kit.ArkData';
async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
console.info('albumGetAssetsDemoCallback');
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
let fetchOption: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.Album = await albumList.getFirstObject();
album.getAssets(fetchOption, (err, albumFetchResult) => {
if (albumFetchResult !== undefined) {
console.info('album getAssets successfully, getCount: ' + albumFetchResult.getCount());
} else {
console.error(`album getAssets failed with error: ${err.code}, ${err.message}`);
}
});
}
getAssets
getAssets(options: FetchOptions): Promise<FetchResult<PhotoAsset>>
Obtains image and video assets. This API uses a promise to return the result.
Atomic service API: This API can be used in atomic services since API version 20.
Required permissions: ohos.permission.READ_IMAGEVIDEO
System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| options | FetchOptions | Yes | Retrieval options. |
Return value
| Type | Description |
|---|---|
| Promise<FetchResult<PhotoAsset>> | Promise used to return the image and video assets obtained. |
Error codes
For details about the error codes, see Universal Error Codes and File Management Error Codes.
In API version 13 and earlier versions, if the caller does not have the required permission, error code 13900012 is returned. Starting from API version 14, the same situation raises error code 201.
| 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 | System inner fail. |
Example
For details about how to create a phAccessHelper instance, see the example provided in photoAccessHelper.getPhotoAccessHelper.
import { dataSharePredicates } from '@kit.ArkData';
import { BusinessError } from '@kit.BasicServicesKit';
async function example(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
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: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let album: photoAccessHelper.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}`);
});
}