这是我的HTML代码
<img [src]="profileImage"/>
<button (click)="openCamera()"> Upload </button>
下面是我的组件代码
import { ViewController, Platform, normalizeURL } from 'ionic-angular';
openCamera() {
let options = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
saveToPhotoAlbum: false,
targetWidth: 400,
targetHeight: 400,
allowEdit: false
}
this.camera.getPicture(options).then((imageData) => {
this.cropService.crop(imageData, { quality: 100 }).then((newImage) => {
this.profileImage = normalizeURL(newImage);
}, (error) => {
console.log("Error cropping image", error);
});
}, (err) => {
console.log('Error camera image', err);
});
}
<img>
标签中答案 0 :(得分:2)
我已经修改了您的代码。请在下面找到更新的代码:
import { ViewController, Platform, normalizeURL } from 'ionic-angular';
openCamera() {
let options = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
saveToPhotoAlbum: false,
targetWidth: 400,
targetHeight: 400,
allowEdit: false
}
this.camera.getPicture(options).then((imageData) => {
this.cropService.crop(imageData, { quality: 100 }).then((newImage) => {
if (this.platform.is('ios')){
this.profileImage = normalizeURL(newImage);
}else{
this.profileImage= "data:image/jpeg;base64," + newImage;
}
}, (error) => {
console.log("Error cropping image", error);
});
}, (err) => {
console.log('Error camera image', err);
});
}
或
如果上述解决方案不起作用,请尝试以下代码:
import { ViewController, Platform, normalizeURL } from 'ionic-angular';
openCamera() {
let options = {
quality: 100,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
saveToPhotoAlbum: false,
targetWidth: 400,
targetHeight: 400,
allowEdit: false
}
this.camera.getPicture(options).then((imageData) => {
// don't need to crop image for iOS platform as it is a in-build feature
if (this.platform.is('ios')) {
this.getFileUri(imageData);
}else { // android platform, we need to do manually
this.cropService.crop(imageData, { quality: 100 }).then(newImage => {
this.getFileUri(newImage);
},
error => console.error('Error cropping image', error)
);
}
}, (err) => {
console.log('Error camera image', err);
});
}
private getFileUri = (url: any) => {
var scope = this;
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var reader = new FileReader();
reader.onloadend = function() {
scope.profileImage = reader.result;
}
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
我认为它与“ cordova-plugin-ionic-webview”插件有关。请参考以下步骤解决该问题:
1]卸载现有的Webview插件
ionic cordova plugin rm cordova-plugin-ionic-webview
2]安装旧版本
ionic cordova plugin add cordova-plugin-ionic-webview@1.2.1
3]干净的cordova android
ionic cordova clean android
请在这里https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/158
中找到更多详细信息答案 1 :(得分:0)
尝试保留原始代码并进行更改
saveToPhotoAlbum: false
到
saveToPhotoAlbum: true