简体   繁体   English

运行 ionic cordova add platform android 后的 Ionic 3,我的 ionic-native/file,filepath,transfer 发生错误

[英]Ionic 3 after running ionic cordova add platform android, error occurs to my ionic-native/file,filepath,transfer

I have the error like in question, when I'm trying to design my application to call native.camera, I see my console in ionic 3 project, I saw this error :我有类似问题的错误,当我尝试设计我的应用程序以调用 native.camera 时,我在 ionic 3 项目中看到了我的控制台,我看到了这个错误:

Native : tried calling Camera.getPicture, but Cordova is not available.本机:尝试调用 Camera.getPicture,但 Cordova 不可用。 Make sure to include cordova.js or run in a device / simulator.确保包含cordova.js 或在设备/模拟器中运行。

F12 错误,来自网络的控制台

Here is the code that I used to called native camera.这是我过去称为原生相机的代码。

This is the code in my problem.html这是我的 issue.html 中的代码

 <button class="logoCamera" ion-button (click)="presentActionSheet()">
    <ion-icon name="camera" ></ion-icon>

This is the code in my problem.ts这是我的问题.ts中的代码

import { File } from '@ionic-native/file';
import { Transfer, TransferObject} from '@ionic-native/transfer';
import { FilePath } from '@ionic-native/file-path';
import { Camera } from '@ionic-native/camera';

public presentActionSheet(){
let actionSheet = this.actionSheetCtrl.create({
  title: 'Select Image',
  buttons: [
    {
      text: 'Load from Library',
      handler: () => {
        this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
      }
    },
    {
      text: 'Use Camera',
      handler: () => {
        this.takePicture(this.camera.PictureSourceType.CAMERA);
      }
    },
    {
      text: 'Cancel',
      role: 'cancel'
    }
  ]
});
actionSheet.present();
}

public takePicture(sourceType){
//Create option for the Camera dialog
var options = {
  quality: 100,
  sourceType : sourceType,
  saveToPhotoAlbum: false,
  correctOrientation: true
};

//Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
  //special handling for android lib
  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('?'));
        this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
      });
  } else {
    var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
    var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/')+ 1);
    this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
  }
}, (err) => {
  this.presentToast('Error while selecting Image.');
});
}

       //Create a new name for image
      private createFileName() {
        var d = new Date(),
        n = d.getTime(),
        newFileName = n + ".jpg";
        return newFileName;
      }

      //copy image to local folder
      private copyFileToLocalDir(namePath, currentName, newFileName) {
        this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
          this.lastImage = newFileName;
        }, error => {
          this.presentToast('Error while storing file.');
        });
      }

    private presentToast(text) {
      let toast = this.toastCtrl.create({
        message: text,
        duration: 3000,
        position: 'middle'
      });
      toast.present();
    }

    public pathForImage(img){
      if (img === null) {
        return '';
      } else {
        return cordova.file.dataDirectory + img;
      }
    }

    public uploadImage() {
      //destination URL
      var url = "";

      //file to upload 
      var targetPath = this.pathForImage(this.lastImage);

      //file name only
      var filename = this.lastImage;

      var options = {
        fileKey: "file",
        fileName: filename,
        chunkedMode: false,
        mimeType: "multipart/form-data",
        params: {'fileName': filename}
      };

      const fileTransfer: TransferObject = this.transfer.create();

      this.loading = this.loadingCtrl.create({
        content: 'Uploading...',
      });
      this.loading.present();

      //use FileTransfer to upload image
      fileTransfer.upload(targetPath, url, options).then(data => {
        this.loading.dismissAll()
        this.presentToast('Image successful uploaded.');
      }, err => {
        this.loading.dismissAll()
        this.presentToast('Error while uploading file.');
      });
    }

When I run ionic serve, everything is smooth, no error, no nothing.当我运行 ionic serve 时,一切都很顺利,没有错误,什么也没有。

But when I click my button to access natve camera, the error shows, please help me figure out the problem, I check a lot of web, and none of it solve my question.但是当我点击我的按钮访问natve相机时,错误显示,请帮我找出问题,我查了很多网页,都没有解决我的问题。

After I try run ionic cordova run ios --simulator, there are error coming out, but I am pretty sure that this error does not exist before I run this command.在我尝试运行 ionic cordova run ios --simulator 后,出现错误,但我很确定在运行此命令之前此错误不存在。

May I know how to solve this problem ??我可以知道如何解决这个问题吗??

在此处输入图片说明

The error message is pretty accurate here:错误消息在这里非常准确:

Native : tried calling Camera.getPicture, but Cordova is not available.本机:尝试调用 Camera.getPicture,但 Cordova 不可用。 Make sure to include cordova.js or run in a device / simulator .确保包含cordova.js在设备/模拟器中运行

Running ionic serve does not include cordova.js nor does it run your application in a simulator or on a device which is why you get the error.运行ionic serve不包含cordova.js,也不在模拟器或设备上运行您的应用程序,这就是您收到错误的原因。 You can fix it either by running your application on the device or simulator:您可以通过在设备或模拟器上运行您的应用程序来修复它:

ionic cordova run android/ios --device/--simulator

Or by adding the browser platform:或者通过添加浏览器平台:

cordova platform add browser

And running the browser platform:并运行浏览器平台:

ionic cordova run browser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM