Possible Causes
Solution
"requestPermissions": [
{
"name": "ohos.permission.CAMERA",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "always"
},
"reason": "$string:Camera_Permission_Request"
},
{
"name": "ohos.permission.MICROPHONE",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "always"
},
"reason": "$string:Camera_Permission_Request"
},
{
"name": "ohos.permission.WRITE_MEDIA",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "always"
},
"reason": "$string:Camera_Permission_Request"
},
{
"name": "ohos.permission.READ_MEDIA",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "always"
},
"reason": "$string:Camera_Permission_Request"
},
// The geographical location permission can be applied for as required. This parameter is optional.
{
"name": "ohos.permission.MEDIA_LOCATION",
"usedScene": {
"abilities": [
"FormAbility"
],
"when": "always"
},
"reason": "$string:Camera_Permission_Request"
}
], onWindowStageCreate(windowStage: window.WindowStage): void {
let atManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(this.context,
[
'ohos.permission.CAMERA',
'ohos.permission.MICROPHONE',
'ohos.permission.MEDIA_LOCATION',
'ohos.permission.READ_MEDIA',
'ohos.permission.WRITE_MEDIA'
]
).then((data) => {
console.info('data:' + JSON.stringify(data));
console.info('data permissions:' + data.permissions);
console.info('data authResults:' + data.authResults);
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/CameraPreviewFlow', (err, data) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
}).catch((err: BusinessError) => {
console.info('data:' + JSON.stringify(err));
});
} async onPageShow() {
let baseContext = this.getUIContext().getHostContext()! as common.BaseContext;
await this.initCamera(baseContext, this.surfaceId);
}
async onPageHide() {
await this.releaseCamera();
} // Obtain the output stream capability supported by the camera device
let cameraOutputCap: camera.CameraOutputCapability =
cameraManager.getSupportedOutputCapability(cameraArray[0], camera.SceneMode.NORMAL_PHOTO);
if (!cameraOutputCap) {
console.error(TAG, "cameraManager.getSupportedOutputCapability error");
return;
}
console.info(TAG, "outputCapability: " +
JSON.stringify(cameraOutputCap)); //The aspect ratio of the preview stream and the video output stream resolution should be consistent
let previewProfilesArray: Array<camera.Profile> = cameraOutputCap.previewProfiles;
let position: number = 0;
if (previewProfilesArray != null) {
previewProfilesArray.forEach((value: camera.Profile,
index: number) => { // View supported preview sizes
console.info(TAG,
`Supported preview sizes: [${value.size.width},${value.size.height},${value.size.width / value.size.height}]`);
if (value.size.width === 2592 && value.size.height === 1200) {
position = index;
}
})
} else {
console.error(TAG, "createOutput photoProfilesArray == null || undefined");
}