Capacitor Camera Api does not pic image from gallery.always open camera

I Used code from official documents but nothing happen they also not responding on this matter.https://capacitor.ionicframework.com/docs/apis/camera but they telling lie to the developer they saying The Camera API allows a user to pick a photo from their photo album or take a picture. On iOS, this uses UIImagePickerController, and on Android this API sends an intent which will be handled by the core Camera app by default.

my code

 async  getPicture(){


       console.log("inside getpicture 1");
     const image = await Camera.getPhoto({
        quality: 90,
       allowEditing: true,
     resultType: CameraResultType.Base64,
    source:CameraSource.Camera
    }).then((imageData) => {
     console.log(imageData);
     let base64Image = 'data:image/jpeg;base64,' + imageData.base64String;
       this.userNewImage = base64Image;
     // this.userNewImage = 
this.sanitizer.bypassSecurityTrustResourceUrl(base64Image);

    }, (err) => {
     // Handle error
    });



  }


  async  getPicture2(){


console.log("inside getpicture 2");
const image = await Camera.getPhoto({
  quality: 90,
  allowEditing: true,
  resultType: CameraResultType.Base64,
  source:CameraSource.Photos,

}).then((imageData) => {
  console.log(imageData);
  let base64Image = 'data:image/jpeg;base64,' + imageData.base64String;
    this.userNewImage = base64Image;
  // this.userNewImage = this.sanitizer.bypassSecurityTrustResourceUrl(base64Image);

  }, (err) => {
  // Handle error
 });

}

  async openImageChooser() {
   if(!this.editable){
    return;
  }
  const actionSheet = await this.actionSheetCtrl.create({
  header: 'Choose image source',
  buttons: [{
    text: 'Camera',
    icon: 'camera',
    cssClass : 'actionsheet-btn',
    handler: () => {
     // this.options.source=CameraSource.Camera;
      this.getPicture();
    }
  }, {
    text: 'Gallery',
    icon: 'image',
    cssClass : 'actionsheet-btn',
    handler: () => {
     // this.options.source=CameraSource.Photos;
      this.getPicture2();
    }
  }, {
    text: 'Cancel',
    role: 'destructive',
    handler: () => {
      // console.log('Share clicked');
    }
  }]
});
  await actionSheet.present();
 }

Please read the documentation carefully before writing harsh words for the community here…
Just blindly copying and pasting the sample codes will not solve your problems.

Change your “source:CameraSource.Camera” to “source:CameraSource.Prompt”

This has been clearly mentioned in the documentation…

// The source to get the photo from. By default this prompts the user to select either the photo album or take a photo. Default: CameraSource.Prompt

CameraSource

enum CameraSource {

Camera: “CAMERA”

Photos: “PHOTOS”

Prompt: “PROMPT”

}

Be polite with your questions. You are requesting for help and not demanding it.

4 Likes

I did it with CameraSource.Prompt but it still goes to gallery instead of prompting user to choose between camera and gallery. But CameraSource.Camera and CameraSource.Photos works properly. By default its going to Camera.