所以我用了cordova-plugin-camera在我的项目中制作照片并从图库中选择。
我的 Android 和 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 没有捕获它。)
最佳答案
所以在 Windows Universal Application 中,您无法在构建 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]);
}
}
关于Windows 10 移动版 - 从图库中选择时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376057/