1. Method One: Using CameraPicker (Official Recommended Approach)
This method utilizes the CameraPicker
API to invoke the secure system camera for photo and video capture.
Implementation Code:
let pathDir = getContext().filesDir; let fileName = `${new Date().getTime()}`; let filePath = pathDir + `/${fileName}.tmp`; fileIo.createRandomAccessFileSync(filePath, fileIo.OpenMode.CREATE); let uri = fileUri.getUriFromPath(filePath); let pickerProfile: picker.PickerProfile = { cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK, saveUri: uri // Path to save the captured image }; let result: picker.PickerResult = await picker.pick( getContext(), [picker.PickerMediaType.PHOTO], pickerProfile ); console.info(`picker resultCode: ${result.resultCode}, resultUri: ${result.resultUri}, mediaType: ${result.mediaType}`); if (result.resultCode === 0) { if (result.mediaType === picker.PickerMediaType.PHOTO) { this.imgSrc = result.resultUri; } }
Key Notes:
- Debug vs. Release Mode: The system camera (
CameraPicker
) must be tested in Release mode during development. Debug mode restricts access to release-only features, causing exceptions. - Permissions: Ensure the app has the
ohos.permission.CAMERA
permission declared inconfig.json
.
2. Method Two: Using startAbility to Launch the Camera App
This method launches the system camera via an ability request (startAbilityForResult
) and handles callbacks through Want
parameters.
Implementation Code:
private async thirdPartyCall(supportMultiMode: boolean) { this.isCrop = false; console.log("thirdPartyCall savePath=" + this.savePath); // Launch camera intent let want: Want = { "action": 'ohos.want.action.imageCapture', "parameters": { supportMultiMode: supportMultiMode, // Important: Callback bundle name must match to access the returned URI callBundleName: "com.example.persontest" } }; // Handle result if (this.context) { let result: common.AbilityResult = await this.context.startAbilityForResult(want); let params = result?.want?.parameters as Record<string, string | number>; let imagePathSrc = params?.resourceUri as string; console.info(this.TAG, 'thirdPartyCall imagePathSrc= ' + imagePathSrc); console.info(this.TAG, 'thirdPartyCall params= ' + JSON.stringify(params)); await this.getImage(imagePathSrc); } }
Key Notes:
- Callback Bundle Name: The
callBundleName
parameter must match the app's bundle name to ensure permission to access the returned image URI. - Result Handling: The captured media URI is retrieved via
result.want.parameters.resourceUri
.
Comparison & Recommendations
Feature |
CameraPicker (Method 1) |
startAbility (Method 2) |
Implementation Complexity |
Lower (simpler API) |
Higher (requires |
Security |
Secure (official API) |
Standard system interaction |
Compatibility |
Recommended for HarmonyOS NEXT |
Works with older versions |
Media Types |
Supports photos and videos |
Primarily for photos (customizable) |
Debug Restrictions |
Requires Release mode for testing |
No such restriction |
Additional Tips
- Video Capture:
- Modify
PickerMediaType.PHOTO
toPickerMediaType.VIDEO
inCameraPicker
for video recording. - For
startAbility
, useohos.want.action.videoCapture
as the action.
- Permissions:
- Declare
ohos.permission.CAMERA
inconfig.json
for both methods. - For video recording, add
ohos.permission.MICROPHONE
.
- Error Handling:
- Implement robust error checks for URI handling and permission requests.
By following these methods, you can effectively integrate camera functionality into your HarmonyOS application while adhering to system security requirements.