• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimedia.cameraPicker (Camera Picker)
2
3The cameraPicker module provides APIs for an application to select a camera to take photos or record videos, depending on the media type specified by the application. The APIs of this module must be called in an UIAbility of the page type. Otherwise, the camera picker cannot be started.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9## Modules to Import
10
11```ts
12import picker from '@ohos.multimedia.cameraPicker';
13```
14
15## pick
16
17pick(context: Context, mediaTypes: Array\<PickerMediaType\>, pickerProfile: PickerProfile): Promise\<PickerResult\>
18
19Starts a camera picker and enters the corresponding mode based on the media type. This API uses a promise to return the result.
20
21**System capability**: SystemCapability.Multimedia.Camera.Core
22
23**Parameters**
24
25| Name         | Type                                             | Mandatory| Description                          |
26| -------------- |-------------------------------------------------| ---- | ---------------------------- |
27| context        | [Context](js-apis-inner-application-context.md) | Yes  | Application context.                  |
28| mediaTypes     | Array\<[PickerMediaType](#pickermediatype)\>    | Yes  | Media type.                   |
29| pickerProfile  | [PickerProfile](#pickerprofile)                 | Yes  | Profile of the camera picker.           |
30
31**Return value**
32
33| Type                                            | Description                                                                                  |
34| ----------------------------------------------- | -------------------------------------------------------------------------------------- |
35| Promise\<PickerResult\>                         | Promise used to return the result, which is specified by [PickerResult](#pickerresult).          |
36
37
38**Example**
39
40```ts
41import picker from '@ohos.multimedia.cameraPicker';
42import camera from '@ohos.multimedia.camera';
43import common from '@ohos.app.ability.common';
44import { BusinessError } from '@ohos.base';
45let mContext = getContext(this) as common.Context;
46
47async function demo() {
48  try {
49    let pickerProfile = {
50      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
51    };
52    let pickerResult: picker.PickerResult = await picker.pick(mContext,
53      [picker.PickerMedaiType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile);
54    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
55  } catch (error) {
56    let err = error as BusinessError;
57    console.error(`the pick call failed. error code: ${err.code}`);
58  }
59}
60```
61
62## PickerMediaType
63
64Enumerates the media types displayed in the camera picker.
65
66**System capability**: SystemCapability.Multimedia.Camera.Core
67
68| Name            | Value   | Description    |
69| ----------------| ----  | ---------|
70| PHOTO           | photo | Photo mode. |
71| VIDEO           | video | Record mode. |
72
73
74## PickerProfile
75
76Defines the configuration information about the camera picker.
77
78**System capability**: SystemCapability.Multimedia.Camera.Core
79
80| Name          | Type                              | Mandatory  | Description        |
81| -------------- | --------------------------------- | ----- | ------------ |
82| cameraPosition       | [CameraPosition](js-apis-camera.md#cameraposition) | Yes   | Camera position.  |
83| saveUri        | string                            | No   | URI for saving the configuration information.|
84| videoDuration  | number                            | No   | Maximum recording duration.|
85
86
87## PickerResult
88
89Defines the processing result of the camera picker.
90
91**System capability**: SystemCapability.Multimedia.Camera.Core
92
93| Name          | Type                               | Mandatory | Description                           |
94| -------------- | ---------------------------------- | ----- | -------------------------------- |
95| resultCode     | number                             | Yes   | Result code. The value **0** means that the processing is successful, and **0** means that the processing fails.|
96| resultUri      | string                             | Yes   | URI of the result. If **saveUri** is empty, **resultUri** is a public media path. If **saveUri** is not empty and the application has the write permission on the URI, the value of **resultUri** is the same as that of **saveUri**. If **saveUri** is not empty and the application does not have the write permission on the URI, **resultUri** cannot be obtained.|
97| mediaType      | [PickerMediaType](#pickermediatype)| Yes   | Media type.                 |
98