Reputation: 422
I'm building an Ionic app and trying to use camera native plugin to take photos and then show it on the screen, script is working when i create a brand new project and put it inside, but it's not working on the project i'm working on.
I thought there is an issue with my package.json, but i updated all packages, i removed file and camera plugins, installed them again but the issue is still the same,
this is the output i get
But when i create a new project and try the script, the photo shows perfectly!
I'm just stuck and do not know what could be making this conflict or where to look, kindly assist
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");
});
}
}
And this is the error i get from the console when i remote debug the app:
(index):1 Refused to load the image 'data:image/jpeg;base64,/9j/4QBAAl+B//2Q==' because it violates the following Content Security Policy directive: "default-src *". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback.
I cut the image base 64 code because it's very long and may not be very comfortable to read, so why it's showing without errors when i create a new project and in this particular project it's not working?
Upvotes: 0
Views: 211
Reputation: 422
Ok, i figured this out so i thought someone might be looking for this
i added this line into my index.html file
<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;
" />
now everything is working perfectly
Upvotes: 1