首页 > 解决方案 > 相机,CameraOptions 在我的移动应用程序中不起作用

问题描述

我正在开发一个移动应用程序,我想打开图库以选择一张图片,但是当单击按钮时,应用程序没有任何作用,甚至没有抛出错误,这是我的代码:


photos: any = '';
base64Image = '';

async agregarImg() {
    const actionSheet = await this.actionSheetController.create({
    header: 'Seleccionar medio',
    buttons: [{
      text: 'Cámara',
      icon: 'camera',
      handler: () => {
        this.takePicture(CameraSource.Camera, false)
      }
    }, {
      text: 'Galería',
      icon: !this.platform.is('ios') ? 'ios-images-outline' : 'images-outline',
      handler: () => {
        this.openGallery();
      }
    }]
    });
    await actionSheet.present();
  }

openGallery() {

    const options: CameraOptions = {
      quality: 50,
      destinationType: this.camera.DestinationType.DATA_URL,
      mediaType: this.camera.MediaType.PICTURE,
      encodingType: this.camera.EncodingType.JPEG,
      correctOrientation: true,
      allowEdit: false,
    }

    this.camera.getPicture(options).then((imageData) => {
      this.base64Image = 'data:image/jpeg;base64,' + imageData;
      this.photos.push(this.base64Image);
      this.photos.reverse();
    }, (err) => {
      console.log(err);
    })
}

我读到了这个问题,可能插件会发生这种情况,这在 Android 上不能完美运行。我使用这个插件: import { Camera, CameraOptions } from '@ionic-native/camera/ngx';

标签: angulartypescriptionic-framework

解决方案


文档中获取,我建议进行以下更改

openGallery() {
   const options: CameraOptions = {
     ...
     destinationType: this.camera.DestinationType. FILE_URI,
     pictureSourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
     ...

推荐阅读