Camera Plugin not opening camera in Ionic 4

和自甴很熟 提交于 2020-03-05 04:36:09

问题


I am working on a project in Ionic 4. I would like user to upload picture by capturing from camera or uploading from library. For now I am running in development mode. The problem is that I am not being able to open camera or photo library on device in debugging mode. However the camera is opening when I run on "DEVAPP". I have tried taking permission but nothing is working out. Here is my code, tried many ways therefore code is a bit scattered:

async selectImage() {
  try {
    const actionSheet = await this.actionSheetController.create({
        header: 'Select Image source',
        buttons: [{
                text: 'Load from Library',
                handler: () => {
                    this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
                }
            },
            {
                text: 'Use Camera',
                handler: () => {
                    this.takePicture(this.camera.PictureSourceType.CAMERA);
                }
            },
            {
                text: 'Cancel',
                role: 'cancel'
            }
        ]
    });
    await actionSheet.present();
  } catch (error) {
    alert(error);
  }
}

takePicture(sourceType: PictureSourceType) {
  const options: CameraOptions = {
      quality: 100,
      sourceType,
      saveToPhotoAlbum: false,
      correctOrientation: true
  };


  alert('i m n takepicture');

  this.androidPermissions.requestPermissions([this.androidPermissions.PERMISSION.CAMERA]).then(
    result => console.log('i am asking for permision: ' +  result),
    err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA)
  );

  if (this.camera.PictureSourceType.CAMERA) {
  this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.CAMERA).then(
  result =>
  this.camera.getPicture(options).then(imagePath => {
      if (this.plt.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
          this.filePath.resolveNativePath(imagePath)
              .then(filePath => {
                  const correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
                  const currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
                  this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
              }). catch((error) => {
                 console.warn('error: ' + error);
              });
      } else {
          const currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
          const correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
          this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
      }
  }),
  err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA));
  } else {
    this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.PHOTOLIBRARY).then(
      result =>
      this.camera.getPicture(options).then(imagePath => {
          if (this.plt.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
              this.filePath.resolveNativePath(imagePath)
                  .then(filePath => {
                      const correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
                      const currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
                      this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
                  });
          } else {
              const currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
              const correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
              this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
          }
      }),
      err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.PHOTOLIBRARY));
  }
}

回答1:


Try this

takePicture(sourceType: PictureSourceType) {
const options: CameraOptions = {
    quality: 100,
    sourceType:sourceType,
    saveToPhotoAlbum: false,
    correctOrientation: true
};
 this.camera.getPicture(options).then(
  imageData => {
   ///DO YOUR LOGIC
  },
  err => { 
    // Handle error
  }
);

}



来源:https://stackoverflow.com/questions/58620150/camera-plugin-not-opening-camera-in-ionic-4

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