我正在构建一个Ionic应用程序,并尝试使用相机本机插件拍照并在屏幕上显示,当我创建一个全新的项目并将其放入其中时,脚本正在工作,但它不适用于我正在从事的项目。
我以为我的package.json存在问题,但是我更新了所有软件包,删除了文件和相机插件,再次安装了它们,但是问题仍然相同, 这是我得到的输出
但是当我创建一个新项目并尝试使用脚本时,照片可以完美显示!
我只是被困住了,不知道是什么原因造成了这种冲突或在哪里寻找,请协助
HTML:
<ion-card *ngIf=resultImageArray>
<img src="{{ resultImageArray }}" />
</ion-card>
TS:
import { Component } from '@angular/core';
import { NavController, ToastController } from 'ionic-angular';
import {Camera, CameraOptions} from "@ionic-native/camera";
import {File} from '@ionic-native/file';
export class HomePage {
public imageURI: any;
public imageName: any;
public fileCreated: boolean = false;
public imageString: any;
resultImageArray: any;
constructor(public navCtrl: NavController, private file: File, private
camera: Camera, private toastCtrl: ToastController,) {}
getImageFromCamera() {
const options: CameraOptions = {
quality: 20,
saveToPhotoAlbum: true,
destinationType: this.camera.DestinationType.FILE_URI,
sourceType: this.camera.PictureSourceType.CAMERA,
encodingType: this.camera.EncodingType.JPEG,
allowEdit: true
};
this.camera.getPicture(options).then((imageData) => {
this.imageURI = imageData;
this.imageName = imageData.substr(imageData.lastIndexOf('/') + 1);
this.file.checkDir(this.file.externalRootDirectory, 'ImagesDemo')
.then(() => {
this.fileCreated = true;
}, (err) => {
console.log("checkDir: Error");
console.log(JSON.stringify(err));
this.presentToast("checkDir Failed");
});
if (this.fileCreated) {
this.presentToast("Directory Already exist");
}
else {
this.file.createDir(this.file.externalRootDirectory,"ImagesDemo",true)
.then((res) => {
this.presentToast("Directory Created");
}, (err) => {
console.log("Directory Creation Error:");
console.log(JSON.stringify(err));
});
}
let tempPath = this.imageURI.substr(0,this.imageURI.lastIndexOf('/')+1);
let androidPath = this.file.externalRootDirectory + '/ImagesDemo/';
this.imageString = androidPath + this.imageName;
this.file.moveFile(tempPath, this.imageName, androidPath,this.imageName)
.then((res) => {
this.presentToast("Image Saved Successfully");
this.readImage(this.imageString);
}, (err) => {
console.log("Image Copy Failed");
console.log(JSON.stringify(err));
this.presentToast("Image Copy Failed");
});
this.toDataURL(this.imageURI, function (dataUrl) {
console.log('RESULT:' + dataUrl);
});
}, (err) => {
console.log(JSON.stringify(err));
this.presentToast(JSON.stringify(err));
});
}
presentToast(msg) {
let toast = this.toastCtrl.create({
message: msg,
duration: 2000
});
toast.present();
}
toDataURL(url, callback) {
let xhr = new XMLHttpRequest();
xhr.onload = function () {
let reader = new FileReader();
reader.onloadend = function () {
callback(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
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.presentToast("Image Get Done");
this.resultImageArray = res;
}, (err) => {
this.presentToast("Image Get Error");
});
}
}
这是我远程调试应用程序时从控制台收到的错误:
(index):1拒绝加载图像'data:image / jpeg; base64,/ 9j / 4QBAAl + B // 2Q ==',因为它违反了以下内容安全策略指令:“ default-src *” 。请注意,未明确设置“ img-src”,因此将“ default-src”用作备用。
我剪切了基于图像的64位代码,因为它很长而且阅读起来可能不太舒服,那么为什么当我创建一个新项目并且在这个特定项目中它无法正常工作时却没有错误显示?
答案 0 :(得分:0)
好吧,我知道了这一点,所以我以为有人可能正在寻找这个
我将此行添加到了index.html文件中
<meta http-equiv="Content-Security-Policy" content="default-src *;
style-src 'self' 'unsafe-inline';
script-src 'self' 'unsafe-inline' 'unsafe-eval';
img-src 'self' data: https://s-media-cache-ak0.pinimg.com;
script-src 'self' https://maps.googleapis.com;
" />
现在一切运行正常