繁体   English   中英

离子 cordova camera.getPicture 不返回任何内容

[英]ionic cordova camera.getPicture returns nothing

我有一个简单的离子应用程序,它从 cordova 相机 function 拍摄照片。它从那里无处可去。

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

  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 function 调用然后迷路。 没有错误或异常。

我已经按照建议尝试了许多解决方案,但没有运气。我已经将 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": []
  }
} 

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

尝试类似下面的方法,我使用此代码从 ionic 应用程序中的相机获取图片(文件路径),在 ios 和 android 中均有效。 有关相机选项的详细信息,您可以查看此处

  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)
    });
  }
  

暂无
暂无

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

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