Cadu Castanha
Cadu Castanha

Reputation: 13

ionic app closes when select more than one image in android with ImagePicker

I'm trying to select multiple images in my ionic app using the ImagePicker plugin, when I select only one image works, but if I select more images the app closes

Versions

ionic: 4.0.3
android: 7.0.0
imagePicker: 2.2.2

Code

getPermission() {

this.imagePicker.hasReadPermission()
  .then(res => {
    if (res) {
      this.openGallery()
    } else {
      this.imagePicker.requestReadPermission()
        .then(res => {
          if (res === 'ok') {
            this.openGallery()
          }
        })
    }
  })
  .catch(error => console.log(error))

}

openGallery() {

let options = {
  maximumImagesCount: 10,
  width: 500,
  height: 500,
  quality: 100,
  outputType: 1
}

this.imagePicker.getPictures(options)
  .then(file => {
    this.images = new Array(file.length);
    for (let i = 0; i < file.length; i++) {
      this.images[i] = 'data:image/jpeg;base64,' + file[i]

    }
  })

}
any help on how to solve this?

Upvotes: 1

Views: 572

Answers (1)

Cristhiano Castilho
Cristhiano Castilho

Reputation: 26

constructor(public navCtrl: NavController,
    private camera: Camera,
    private transfer: FileTransfer,
    private file: File,
    private fileOpener: FileOpener,
    private loadingCtrl: LoadingController,
    private plt: Platform,
    private imagePicker: ImagePicker,
    private base64: Base64,
    private sanitizer: DomSanitizer) {
    }
    images=[];
    public items: Array<{ images: string;}> = [];
    takePhoto(){
      this.imagePicker.hasReadPermission()
      .then(res => {
        if (res) {
          this.openGallery();
        } else {
          this.imagePicker.requestReadPermission()
          .then(res => {
            if (res === 'ok') {
              this.openGallery();
            }
          })
        }
      })
      .catch(error => console.log(error));
    }
    openGallery () {
      let options = {
        maximumImagesCount: 10,
        correctOrientation: true,
        quality: 30,
        width: 100,
        height: 100,
        allowEdit : true,
        outputType: 1,
      }
      this.imagePicker.getPictures(options)
      .then(file => {
        //this.images = new Array(file.length);
        for (let i = 0; i < file.length; i++) {
        //this.images[i] = 'data:image/jpeg;base64,' + file[i]
        this.images.push('data:image/jpeg;base64,' +file[i]);
      }
    });
  }

I've been looking for a solution just like yours for me ... your code is perfect ... see the small changes uqe made by adding ";" and reducing image size and quality ... Congratulations and thank you for helping me! See on your phone if you use the way to kill all APP as soon as you get out of it, because it can also be a cause for when you access the camera the app stops, because it dies ....

Upvotes: 1

Related Questions