Windows 10 Mobile - 应用程序在从图库中选择时崩溃
问题描述:
所以我在我的项目中使用cordova-plugin-camera来制作照片并从图库中进行选择。Windows 10 Mobile - 应用程序在从图库中选择时崩溃
我的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();
});
而在第二页组件我有,什么被执行之前,通常会显示imageURI警报传递。
无论是成功还是错误回调都不会执行,应用程序只会关闭。
我试着在它周围加入一个try-catch,但它似乎不会引发错误。 (至少,抓不到它)。
答
因此,在Windows通用应用程序中,当构建Cordova应用程序(至少我还没有找到如何)时,无法访问照片库。由于对文件的读/写访问权限。 (想象一下,在桌面上打开一个Windows 10应用程序,它只是让你所有的照片排成一排,这不是一个好主意)。
解决方法是使用文件输入并在点击按钮时触发它(因此您的UI不会更改)。这样用户就可以为他/她自己选择文件,并为该应用程序提供对该文件的读/写访问权限。
的HTML看起来有点像这样:
<input type="file" hidden #file (change)="fileChanged()"></input>
<button (click)="file.click()">My custom button</button>
,并读取到从FileInput的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]);
}
}
你检查吗? https://github.com/apache/cordova-plugin-camera#windows-quirks –
@suraj是的,那是WP8和WP8.1。我已经找到了导致它的原因,并会在今天晚些时候写出一个答案。虽然Windows Universal所需的解决方法与WP8(.1)相同, – Ivaro18