从图片库获取图片

Getting image from image library

离子 3,Angular CLI 7,Angular 5.

无法从库中获取图像。

调用 getPicture 失败。我收到一个错误:

“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)”

我已经尝试将所有本机插件放入 5.0.0-beta.15 版本,但没有帮助。

我尝试过使用 ImagePicker 和 Camera 插件,但都出现相同的错误。

ImagePicker(插件为@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); });

相机(插件为@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);
});

有人能帮帮我吗?

UPD:我将 ImagePicker 和 Base64 降级到与 native-core 相同的版本,它似乎开始工作了。但是当我尝试 运行 来自 Ionic DevApp 的应用程序时,它一直告诉我应该将 telerik-imagepicker 插件添加到 cordova,但它已经在它的列表中了! (是的,我确实尝试添加)。所以错误是"plugin_not_installed".

UPD 2:我不得不离开这个项目,所以不幸的是我不知道解决方案是什么,因为我没有检查 Sergey 提出的想法

通读给我的答案后,似乎无法正确安装依赖项,或者在构建应用程序时缺少对它们的引用。代码乍一看似乎是正确的。您无论如何都需要将 ionic 升级到版本 4,因为在其他一些帖子中也看到 ionic v3 的相机存在问题。 "ionic 3 does not support those plugins"

这个问题有类似的问题,通过重新安装正确的依赖版本得到解决

否则,如果您只在从头开始制作的小应用程序中隔离相机,它是否仍然会在 V4 中产生相同的错误? (仅支持的版本)

由于您使用的是 Ionic 3。您需要确保根据文档安装和导入插件:

请注意这是针对 Ionic 4 的: https://ionicframework.com/docs/native/image-picker

安装说明:

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

install @ionic-native/image-picker

这是 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

请注意帮助 npm 安装 Ionic v3 相关插件的@4。

另请注意,导入语句会有所不同:

Ionic 4 具有:

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

Ionic 3 具有:

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

删除插件:

cordova plugin list

然后

cordova plugin remove PLUGIN_NAME

然后为您的框架版本安装合适的插件。