我正在尝试在android中打开前后摄像头,这是我打开摄像头的功能:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
photoUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".provider", photoFile);
} else {
photoUri = Uri.fromFile(photoFile);
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
if (formElement.getElemntType().equalsIgnoreCase("FRONT_CAMERA")) {
takePictureIntent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true);
takePictureIntent.putExtra("android.intent.extras.CAMERA_FACING", android.hardware.Camera.CameraInfo.CAMERA_FACING_FRONT);
} else {
takePictureIntent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_BACK);
}
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
我有一个来自服务器的表单元素对象。 基于该对象的属性,我需要打开后置摄像头和前置摄像头。 我已在清单文件中添加了摄像头权限,并在清单中的行下方添加了
android:name="android.hardware.camera"
android:required="false" />
android:name="android.hardware.camera.front"
android:required="false" />
请建议我该如何处理这种情况。