Ionic App crash after Open native camera - Error 20

久未见 提交于 2019-12-24 01:17:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!