Search code examples
angularionic-frameworkionic3ionic-native

Getting image from image library


Ionic 3, Angular CLI 7, Angular 5.

Not able to get an image from the library.

It fails on calling getPicture. I’m getting an error:

“Object(WEBPACK_IMPORTED_MODULE_1__ionic_native_core[“cordova”]) is not a function. (In ‘Object(WEBPACK_IMPORTED_MODULE_1__ionic_native_core[“cordova”])(this, “getPicture”, { “callbackOrder”: “reverse” }, arguments)’, ‘Object(WEBPACK_IMPORTED_MODULE_1__ionic_native_core[“cordova”])’ is an instance of Object)”

I already tried putting all native plugins to version 5.0.0-beta.15 but it doesn’t help.

I've tried both with ImagePicker and Camera plugins but both give same error.

ImagePicker (the plugin is @ionic-native/image-picker) :

let options = {
  maximumImagesCount: 1
};
this.imagePicker.getPictures(options).then((results) => {
  for (var i = 0; i < results.length; i++) {
      this.imgPreview = results[i];
      this.base64.encodeFile(results[i]).then((base64File: string) => {
        this.regData.avatar = base64File;
      }, (err) => {
      });
  }
}, (err) => { console.log(err); });

Camera (the plugin is @ionic-native/camera):

var sourceType = this.camera.PictureSourceType.PHOTOLIBRARY;
var options = {
  quality: 100,
  sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
  saveToPhotoAlbum: false,
  correctOrientation: true
};
this.camera.getPicture(options).then((imagePath) => {
  console.log("Img path: " + imagePath);
  if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
    this.filePath.resolveNativePath(imagePath)
      .then(filePath => {
        let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
        let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
      });
  } else {
    var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
    var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
  }
}, (err) => {
  console.log(err);
});

Could anyone help me please?

UPD: I downgraded ImagePicker and Base64 to same version as native-core and it seemed to start working. But when I tried to run the app from Ionic DevApp it kept telling me that I should add telerik-imagepicker plugin to cordova but it's already on its list! (yes, I did try to add though). So the error is "plugin_not_installed".

UPD 2: I had to leave this project so unfortunately I don't know what is the solution since I didn't check the idea proposed by Sergey


Solution

  • Since you are using Ionic 3. You need to make sure you install and import plugins according to documentation:

    Please note this is for Ionic 4: https://ionicframework.com/docs/native/image-picker

    Install instructions:

    ionic cordova plugin add cordova-plugin-telerik-imagepicker npm

    install @ionic-native/image-picker

    This is for Ionic 3:

    ionic cordova plugin add cordova-plugin-telerik-imagepicker --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="your usage message" npm install --save @ionic-native/image-picker@4

    Please note @4 that helps npm to install relevant plugin for Ionic v3.

    Also note that import statements will differ:

    Ionic 4 has:

    import { ImagePicker } from '@ionic-native/image-picker/ngx';
    

    Ionic 3 has:

    import { ImagePicker } from '@ionic-native/image-picker';
    

    To remove plugins:

    cordova plugin list

    then

    cordova plugin remove PLUGIN_NAME

    then install proper plugin for your framework version.