【问题标题】:Windows 10 Mobile - App crashes when selecting from the galleryWindows 10 移动版 - 从图库中选择时应用程序崩溃
【发布时间】:2018-01-04 16:11:39
【问题描述】:

所以我在我的项目中使用了cordova-plugin-camera 来制作照片并从图库中选择。

我的安卓和 iOS 应用完全没有任何问题。但在实时 Windows 10 移动设备上对其进行测试时,我的应用在我从图库中选择图像后崩溃。

相机确实可以工作。

我使用的代码(简化)

this.camera.getPicture({
  quality: 50,
  destinationType: this.camera.DestinationType.FILE_URI,
  sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE,
  saveToPhotoAlbum: false,           //gave problems in Windows
  correctOrientation: true
}).then(imageURI => {
  this.navCtrl.push(Page2, {image: imageURI});
}, err => {
  // ionic alert, way of still showing alerts to Windows users
  this.alertCtrl.create({message: err}).present();
});

在 Page2 组件中,在执行任何操作之前,我有一个警报,通常会显示已通过的 imageURI。

成功和错误回调都没有执行,应用程序只是关闭。

我尝试在它周围添加一个 try-catch,但它似乎不会引发错误。 (至少,catch 没有抓住它。)

【问题讨论】:

  • @suraj 是的,这适用于 WP8 和 WP8.1。我已经找到了导致它的原因,并将在今天晚些时候写一个答案。尽管 Windows Universal 所需的解决方法与 WP8(.1) 相同,但问题不同

标签: windows cordova ionic2 win-universal-app windows-10-universal


【解决方案1】:

因此,在 Windows 通用应用程序中,您在构建 Cordova 应用程序时无法访问照片库(至少,我还没有找到方法)。因为对文件的读/写访问。 (想象一下,在您的桌面上打开一个 Windows 10 应用程序,它只是为您排好所有照片,这不是一个好主意)。

一种解决方法是使用文件输入并在单击按钮时触发它(因此您的 UI 不会改变)。这样,用户就可以为他/她/自己选择文件,并为应用程序提供对该文件的读/写访问权限。

html 看起来有点像这样:

<input type="file" hidden #file (change)="fileChanged()"></input>
<button (click)="file.click()">My custom button</button>

并从文件输入中读取 src(作为 dataURI):

fileChange(event) {
    // show loading screen, this is not very fast on UWP
    let loading = this.loadingCtrl.create({
      content: 'Processing photo...'
    });
    loading.present();

    let fileList: FileList = event.target.files;
    if(fileList.length > 0) {
      let file: File = fileList[0];
      var reader = new FileReader();

      // define callback seperate from onload, else e.target.result is undefined
      let callback = (e) => { 
         loading.dismiss(); 
         let src = e.target.result;
      };

      reader.onload = callback;

      reader.readAsDataURL(event.target.files[0]);
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多