android - 相机照片未显示离子 3
问题描述
我尝试显示用我的相机拍摄的照片。
这是我的 HTML:
<ion-grid>
<ion-row>
<ion-col col-6>
<button ion-button full (click)="prendreUnePhoto()">Prendre une photo</button>
</ion-col>
<ion-col col-6>
<button ion-button full (click)="choisirUneImage()">Choisir une image</button>
</ion-col>
</ion-row>
<ion-row>
<ion-col col-12>
<button ion-button full (click)="envoiMail()">Envoyer Email</button>
</ion-col>
</ion-row>
<ion-card>
<ion-card-header>Image</ion-card-header>
<ion-card-content>
<img [src]="currentImage" *ngIf="currentImage"/>
</ion-card-content>
</ion-card>
然后,这是我的 TypeScript :
currentImage = null
constructor(public navCtrl: NavController, public navParams: NavParams, private emailComposer : EmailComposer, private camera: Camera) {
}
prendreUnePhoto() {
const options: CameraOptions = {
sourceType: this.camera.PictureSourceType.CAMERA,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
this.currentImage = imageData;
}, (err) => {
// Handle error
console.log('Image error: ', err);
});
}
envoiMail(){
let email = {
to : 'bastien.roussel.19@gmail.com',
subject : '[IONIC] test mail',
body : 'Premier <b>email</b> de test depuis ionic 3',
attachments: [
this.currentImage
],
isHtml : true
};
this.emailComposer.open(email);
}
所以,我想在我的卡片上显示我的照片。问题是什么 ?我遵循了许多不同的教程,阅读了 Ionic 文档,但没有找到解决方案。
我不得不说:当我单击“Envoyer Email”按钮(发送电子邮件)时,我的照片会正确显示在我的邮件中,并带有此代码。但是如果我使用,我的图像不会显示在我的邮件中:
this.currentImage = 'data:image/jpeg;base64,' + imageData;
谢谢您的帮助。
解决方案
您不能直接从本地路径显示图像。在您的设备中。相反,您需要从本地路径读取该图像,然后将该数据保存到任何变量中,然后将该变量作为 src 分配给 img 标签。
a 用简单的功能做到了这一点,如下
public currentImage**strong text**: any;
readImage(filePath) {
let tempPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let imageName = filePath.substr(filePath.lastIndexOf('/') + 1);
this.file.readAsDataURL(tempPath, imageName)
.then((res) => {
this.currentImage= res;
}, (err) => {
});
}
这对我有用...
推荐阅读
- java - 从单独运行的 jar 中获取字段
- kubernetes - 无法定义端口的值范围
- git - 如何在 GitHub Desktop 中使用 SSH 远程?
- javascript - 仅允许通过 iFrame 访问 URL,并防止在 PHP 中直接访问
- python - pandas DataFrame groupby 就地填充
- c++ - OpenBLAS 只为一个例程设置线程数
- javascript - 使用(Javascript)动态切换到不同的firebase项目应用程序
- python - 熊猫:尝试为数据框列表连续编辑数据
- neo4j - 如何使用可变长度关系和每个级别的过滤器查询 Neo4j N 级别的深度
- angular - Angular 8 和材料缺失