问题
I'm using the cordova camera plugin on ionic 4 to capture some image.
takePicture() {
console.log(' camera takePicture ');
const options: CameraOptions = {
quality: 100,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
this.selectedImage = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
// Handle error
console.log('Camera issue:' + err);
});
}
The application doesent crash but this code always return Camera issue: 20 and the camera interface never appears on the phone screen.
I tried to modify the config.xml file like this because i thought it was problem with android permissions but stil not working:
<config-file mode="merge" parent="/*" target="AndroidManifest.xml">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<!-- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
<!-- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->
</config-file>
I had to comment the storage because it crashed the appllication in the same line.
Also, I tried to uninstall and reinstal the plugin
ionic cordova plugin remove cordova-plugin-camera
npm uninstall @ionic-native/camera
ionic cordova plugin add cordova-plugin-camera
npm install @ionic-native/camera
I have no idea how to solve this, I need some help please. Thanks!
回答1:
You should use the cordova-plugin-camera-preview
plugin to manipulate camera in ionic. Note that this plugin is only compatible with Android and iOS.
Install it with:
ionic cordova plugin add cordova-plugin-camera-preview
npm install @ionic-native/camera-preview
Usage:
// camera options (Size and location). In this example, the preview uses the rear camera and displays the preview in the back of the webview
const cameraPreviewOpts: CameraPreviewOptions = {
x: 0,
y: 0,
width: window.screen.width,
height: window.screen.height,
camera: 'rear',
tapPhoto: true,
previewDrag: true,
toBack: true,
alpha: 1
}
// start camera
this.cameraPreview.startCamera(cameraPreviewOpts).then(
(res) => { console.log(res) },
(err) => { console.log(err) }
);
来源:https://stackoverflow.com/questions/55711060/ionic-app-crash-after-open-native-camera-error-20