Class (PhotoViewPicker)
PhotoViewPicker provides APIs for the user to select images and videos. Before using the APIs of PhotoViewPicker, you need to create a PhotoViewPicker instance.
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';
select
select(option?: PhotoSelectOptions) : Promise<PhotoSelectResult>
Starts a photoPicker page for the user to select one or more images or videos. This API uses a promise to return the result. You can pass in PhotoSelectOptions to specify the type and maximum number of the files to select. A PhotoSelectResult object is returned.
NOTE
photoUris in the PhotoSelectResult object returned by this API has permanent authorization and can be used only by calling photoAccessHelper.getAssets. For details, see Using a Media File URI.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| option | PhotoSelectOptions | No | Options for selecting files. If this parameter is not specified, up to 50 images and videos are selected by default. |
Return value
| Type | Description |
|---|---|
| Promise<PhotoSelectResult> | Promise used to return information about the images or videos selected. |
Error codes
For details about the error codes, see Universal Error Codes, File Management Error Codes, and Media Library Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 13900042 | Unknown error. |
| 23800151 | Scene parameters validate failed, possible causes:1. An illegal enumeration value was passed to PhotoSelectOptions.globalMovingPhotoState. Only MOVING_PHOTO_ENABLE and MOVING_PHOTO_DISABLE are supported for configuration; |
Example
import { BusinessError } from '@kit.BasicServicesKit';
async function example01(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
try {
let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
photoSelectOptions.maxSelectNumber = 5;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(photoSelectOptions).then((photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
console.info('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult));
}).catch((err: BusinessError) => {
console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`PhotoViewPicker failed with err: ${err.code}, ${err.message}`);
}
}
select
select(option: PhotoSelectOptions, callback: AsyncCallback<PhotoSelectResult>) : void
Starts a photoPicker page for the user to select one or more images or videos. This API uses an asynchronous callback to return the result. You can pass in PhotoSelectOptions to specify the media file type and the maximum number of files to select.
NOTE
photoUris in the PhotoSelectResult object returned by this API has permanent authorization and can be used only by calling photoAccessHelper.getAssets. For details, see Using a Media File URI.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| option | PhotoSelectOptions | Yes | Options for selecting images or videos. |
| callback | AsyncCallback<PhotoSelectResult> | Yes | Callback used to return information about the images or videos selected. |
Error codes
For details about the error codes, see Universal Error Codes, File Management Error Codes, and Media Library Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
| 13900042 | Unknown error. |
| 23800151 | Scene parameters validate failed, possible causes:1. An illegal enumeration value was passed to PhotoSelectOptions.globalMovingPhotoState. Only MOVING_PHOTO_ENABLE and MOVING_PHOTO_DISABLE are supported for configuration; |
Example
import { BusinessError } from '@kit.BasicServicesKit';
async function example02(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
try {
let photoSelectOptions = new photoAccessHelper.PhotoSelectOptions();
photoSelectOptions.MIMEType = photoAccessHelper.PhotoViewMIMETypes.IMAGE_TYPE;
photoSelectOptions.maxSelectNumber = 5;
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select(photoSelectOptions, (err: BusinessError, photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
if (err) {
console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
return;
}
console.info('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult));
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`PhotoViewPicker failed with err: ${err.code}, ${err.message}`);
}
}
select
select(callback: AsyncCallback<PhotoSelectResult>) : void
Starts a photoPicker page for the user to select one or more images or videos. This API uses an asynchronous callback to return the result.
NOTE
photoUris in the PhotoSelectResult object returned by this API has permanent authorization and can be used only by calling photoAccessHelper.getAssets. For details, see Using a Media File URI.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.FileManagement.PhotoAccessHelper.Core
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| callback | AsyncCallback<PhotoSelectResult> | Yes | Callback used to return information about the images or videos selected. |
Error codes
For details about the error codes, see Universal Error Codes and File Management Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 13900042 | Unknown error. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
async function example03(phAccessHelper: photoAccessHelper.PhotoAccessHelper) {
try {
let photoPicker = new photoAccessHelper.PhotoViewPicker();
photoPicker.select((err: BusinessError, photoSelectResult: photoAccessHelper.PhotoSelectResult) => {
if (err) {
console.error(`PhotoViewPicker.select failed with err: ${err.code}, ${err.message}`);
return;
}
console.info('PhotoViewPicker.select successfully, photoSelectResult uri: ' + JSON.stringify(photoSelectResult));
});
} catch (error) {
let err: BusinessError = error as BusinessError;
console.error(`PhotoViewPicker failed with err: ${err.code}, ${err.message}`);
}
}
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 capi-mediaassetmanager-oh-mediaassetmanager
openharmony 鸿蒙 arkts-apis-photoAccessHelper-MediaAssetProgressHandler
openharmony 鸿蒙 arkts-apis-photoAccessHelper-AbsAlbum
openharmony 鸿蒙 arkts-apis-photoAccessHelper-PhotoAccessHelper
openharmony 鸿蒙 arkts-apis-photoAccessHelper-QuickImageDataHandler
openharmony 鸿蒙 capi-media-access-helper-capi-h
openharmony 鸿蒙 arkts-apis-photoAccessHelper-PhotoAsset
openharmony 鸿蒙 capi-mediaassetmanager-oh-movingphoto
openharmony 鸿蒙 capi-mediaassetmanager
openharmony 鸿蒙 capi-mediaassetmanager-medialibrary-requestid