【问题标题】:ionic cordova camera.getPicture returns nothingionic cordova camera.getPicture 什么也不返回
【发布时间】:2020-07-21 17:34:08
【问题描述】:

我有一个简单的 ionic 应用程序,它从科尔多瓦相机功能中拍照。它从那里无处可去..

真的需要帮助来解决这个问题..我从几个星期开始就坚持这个..现在..

  const cameraOptions: CameraOptions =  {
      quality: 100,
      destinationType: this.camera.DestinationType.FILE_URI,
      // encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      sourceType: this.camera.PictureSourceType.CAMERA,
      saveToPhotoAlbum : true
     }
     try {
       let obj = this.camera.getPicture(cameraOptions).then((imageData) => {
    // never entered here
         console.log('getPicture Success '); 
         // let base64Image = 'data:image/jpeg;base64,' + imageData;
         let base64Image = imageData;
         this.capturedSnapURL = base64Image;
         alert(this.capturedSnapURL);
       }, (err) => {
         alert('error=' + err); // never entered
         console.log(err);
       });
      alert('no error but no picture:' + obj); // always print this [object Promise]
    } catch (error) { 
      alert('exception error' + error); //never entered here
    } 

我一直在尝试上面的代码,它从来没有进入成功块,也没有进入 getPicture 的 then() 的错误块。从未打印过“getPicture Success”或错误。
它只显示“没有错误但没有图片:[object Promise]”消息。 我也尝试在模拟器中使用 remotedevice 选项进行调试。它进入直到 getPicture 函数调用然后丢失。没有错误或异常。

我按照建议尝试了许多解决方案,但没有运气。我已将 ionic、angular、cordova、nodejs 升级到最新版本以解决此问题,但没有运气。这里是版本详情

Ionic:
   ionic (Ionic CLI)             : 4.12.0 (/usr/lib/node_modules/ionic)
   Ionic Framework               : @ionic/angular 5.2.3
   @angular-devkit/build-angular : 0.1000.3
   @angular-devkit/schematics    : 10.0.3
   @angular/cli                  : 10.0.3
   @ionic/angular-toolkit        : 2.2.0
Cordova:
   cordova (Cordova CLI) : 9.0.0 (cordova-lib@9.0.1)
   Cordova Platforms     : android 8.1.0
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 3.1.2, (and 11 other plugins)

System:
   Android SDK Tools : 26.1.1 (...~/Android/Sdk)
   NodeJS            : v12.18.2 (~/node-v12.18.2-linux-x64/bin/node)
   npm               : 6.13.4
   OS                : Linux 3.10

请注意,我一直在 android 模拟器上尝试这个。 这是我的 package.json。

{
  "name": "mygrd",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^10.0.4",
    "@angular/core": "^10.0.4",
    "@angular/forms": "^10.0.4",
    "@angular/http": "^7.2.16",
    "@angular/platform-browser": "^10.0.4",
    "@angular/platform-browser-dynamic": "^10.0.4",
    "@angular/router": "^10.0.4",
    "@ionic-native/android-permissions": "^5.27.0",
    "@ionic-native/camera": "^5.27.0",
    "@ionic-native/core": "^5.26.0",
    "@ionic-native/http": "^5.27.0",
    "@ionic-native/splash-screen": "^5.26.0",
    "@ionic-native/status-bar": "^5.26.0",
    "@ionic-native/uid": "^5.26.0",
    "@ionic-native/unique-device-id": "^5.26.0",
    "@ionic/angular": "^5.2.3",
    "@ionic/angular-toolkit": "^2.2.0",
    "@ionic/pro": "2.0.4",
    "cordova-android": "^8.1.0",
    "cordova-hot-code-push-plugin": "1.5.3",
    "cordova-plugin-android-permissions": "1.0.2",
    "cordova-plugin-camera": "4.1.0",
    "cordova-plugin-device": "^2.0.3",
    "cordova-plugin-file": "6.0.2",
    "cordova-plugin-filepath": "1.5.8",
    "cordova-plugin-ionic-keyboard": "^2.2.0",
    "cordova-plugin-ionic-webview": "^3.1.2",
    "cordova-plugin-splashscreen": "^5.0.4",
    "cordova-plugin-statusbar": "^2.4.3",
    "cordova-plugin-unique-device-id2": "2.0.0",
    "cordova-plugin-uniquedeviceid": "1.3.2",
    "cordova-plugin-whitelist": "^1.3.4",
    "cordova-sqlite-storage": "5.0.0",
    "core-js": "^2.6.11",
    "rxjs": "~6.6.0",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/architect": "^0.1000.3",
    "@angular-devkit/build-angular": "^0.1000.3",
    "@angular-devkit/core": "^10.0.3",
    "@angular-devkit/schematics": "^10.0.3",
    "@angular/cli": "^10.0.3",
    "@angular/compiler": "^10.0.4",
    "@angular/compiler-cli": "^10.0.4",
    "@angular/language-service": "^10.0.4",
    "@ionic/app-scripts": "^3.2.4",
    "@types/jasmine": "^2.8.17",
    "@types/jasminewd2": "^2.0.8",
    "@types/node": "^10.12.30",
    "codelyzer": "~4.5.0",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~3.1.4",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^2.0.6",
    "karma-jasmine": "~1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.4.4",
    "ts-node": "^8.0.3",
    "tslint": "~5.12.0",
    "typescript": "~3.9.7"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-plugin-whitelist": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-plugin-ionic-keyboard": {},
      "cordova-hot-code-push-plugin": {},
      "cordova-plugin-file": {},
      "cordova-sqlite-storage": {},
      "cordova-plugin-filepath": {},
      "cordova-plugin-android-permissions": {},
      "cordova-plugin-camera": {
        "ANDROID_SUPPORT_V4_VERSION": "27.+"
      }
    },
    "platforms": []
  }
} 

【问题讨论】:

  • 您是否在 Xcode 调试器或 Android 的 logcat 中获取了一些相关日志?
  • 不确定 xcode 调试器..但我已经使用 Chrome 调试器的远程设备选项(使用 Inspect )连接到 Android Emulator 并检查对象和日志等。逐步跟踪流程。控制台或日志中甚至没有错误。 getPicture 调用之前的所有预期步骤,然后它就丢失了。
  • 如果 Chrome 检查器/Safari 没有抛出任何东西,这是您获得有关正在发生的事情的一些信息的唯一方法。

标签: android angular cordova ionic-framework camera


【解决方案1】:

对于这个问题的任何输入都非常感谢...? 不幸的是,由于这个错误,我无法在 ionic/cordova 框架上使用相机,如果不工作可能不得不迁移到 android studio..

【讨论】:

  • 您是否以某种方式解决了这个问题?我也有同样的问题!
【解决方案2】:

尝试如下操作,我使用此代码从 ionic 应用程序中的相机获取图片(文件路径),适用于 ios 和 android。相机选项详情可以看here

  constructor(
    private camera         : Camera,
    private platform       : Platform,
    private imagePicker    : ImagePicker,
    private filePath       : FilePath,
  ) {}

  takePicture() {
    var options: CameraOptions =
    {
      quality             : 100,
      sourceType          : this.camera.PictureSourceType.CAMERA,
      saveToPhotoAlbum    : true,
      correctOrientation  : true,
      encodingType        : this.camera.EncodingType.JPEG,
      targetHeight        : 1000,
      targetWidth         : 1000,
    };
      
    this.camera.getPicture(options).then(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('?'));
            console.log('filePath 1', filePath);
            console.log('current  1', currentName);
            console.log('correct  1', correctPath);
          });
        } else {
          let currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
          let correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
          console.log('filePath 2', filePath);
          console.log('current  2', currentName);
          console.log('correct  2', correctPath);
        }
    }, err1 => {
      console.log('something wrong 1 -> ', err1);
    }).catch(err2 => {
      console.log('something wrong 2 -> ', err2)
    });
  }
  

【讨论】:

  • 对不起..不走运..它从来没有进入“then”。没有错误或登录。从未进入捕获。它只是在 getPicture 之后丢失了。我猜一些配置仍然丢失可能与相机插件或其他东西有关。我想知道它与权限有关..
猜你喜欢
  • 2014-05-14
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2015-02-06
相关资源
最近更新 更多