Camera Plugin не открывает камеру в Ionic 4 - PullRequest
0 голосов
/ 30 октября 2019

Я работаю над проектом в Ionic 4. Я бы хотел, чтобы пользователь загружал изображение, снимая с камеры или загружая из библиотеки. Сейчас я работаю в режиме разработки. Проблема в том, что я не могу открыть камеру или библиотеку фотографий на устройстве в режиме отладки. Однако камера открывается, когда я работаю на "DEVAPP". Я пытался получить разрешение, но ничего не получается. Вот мой код, перепробовал много способов, поэтому код немного разбросан:

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 Ответ

0 голосов
/ 01 ноября 2019

Попробуйте это

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
  }
);

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...