How to attach images from both camera and photolibray to email with Email composer?


#1

Hi,

I make making an Ionic 2 app which uses Email Composer native plugin. I want to attach images from CAMERA and PHOTOLIBRARY for which am using the native Camera plugin. I want to store the images selected to an array and attach it the email composer. The problem is i am unable to display this array on images on the app and also was unable to attach images to email as it either shows “permission denied” or the app crashes. below is the part of the code i am using for images attaaching


Code to take image from camera

capture() {
 const options: CameraOptions = {
      quality: 20,
      destinationType: this.camera.DestinationType.FILE_URI,
      sourceType: this.camera.PictureSourceType.CAMERA,
      encodingType: this.camera.EncodingType.JPEG,
      allowEdit: false
    }
    this.camera.getPicture(options).then((imageData) => {
      this.images.push(imageData);
      // console.log(imageData);
      this.createFileEntry(imageData);
    }, (err) => {
      console.log(err.message);
    });
  }

Code to take image from photo library

  selectFromGallery() {
    const options: CameraOptions = {
      quality: 20,
      destinationType: this.camera.DestinationType.FILE_URI,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      allowEdit: false,
    }
    this.camera.getPicture(options).then((imageData) => {
      // this.currentImage = imageData;
      // console.log(imageData);
      // this.images.push(imageData);
      this.createFileEntry(imageData);
    }, (err) => {
      console.log(err.message);
    });
  }

Below is the code to copy file to external data directory to attach images to email ( Following from this link )

Code to copy the images and push to images array

  createFileEntry(fileURI) {
    this.file.resolveLocalFilesystemUrl(fileURI).then((fileEntry) => {
      console.log(fileEntry);
      var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
      var newName = this.makeid() + name;
      this.file.resolveDirectoryUrl(this.file.externalDataDirectory).then((fileSystem2) => {
        fileEntry.copyTo(fileSystem2, newName, (entry) => {
          this.zone.run(() => {
            console.log(entry);
            this.images.push(entry.nativeURL);
          });

        }, (error) => {
          console.log("fail: " + error.message);
        });
      });

    });
  }

Can anyone give me a working example of code with image attachments to email from both camera and photolibray.

Thank you