Hello,
I m trying to copy the file from PHOTOLIBRARY to cordova.file.dataDirectory to my local storage.
The file is getting copied as i ran the below command and checked
adb shell > run-as io.ionic.starter > cd files > ls > copiedfile.jpg
But when i try to view, it shows broken image.
below is my code and link which i followed to copy my file.
link : https://devdactic.com/ionic-2-images/
code:
public presentActionSheet() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Select Image Source',
buttons: [
{
text: 'Load from Library',
handler: () => {
this.takePicture(this.camera.PictureSourceType.PHOTOLIBRARY);
}
},
{
text: 'Use Camera',
handler: () => {
this.takePicture(this.camera.PictureSourceType.CAMERA);
}
},
{
text: 'Cancel',
role: 'cancel'
}
]
});
actionSheet.present();
}
public takePicture(sourceType) {
// Create options for the Camera Dialog
var options = {
quality: 100,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
// Special handling for Android library
if (sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.checkAndCreateDir(correctPath, currentName, this.user.username+"_avatar.jpg");
});
} else {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.checkAndCreateDir(correctPath, currentName, this.user.username+"_avatar.jpg");
}
}, (err) => {
this.presentToast('Error while selecting image.');
});
}
private checkAndCreateDir(namePath, currentName, newFileName) {
this.dir = "";
this.copyFileToDir(namePath, currentName, newFileName, this.dir);
}
private copyFileToDir(namePath, currentName, newFileName, dir){
// this.common.showError("Response", cordova.file.dataDirectory+dir);
this.file.copyFile(namePath, currentName, cordova.file.dataDirectory+dir , newFileName).then(success => {
this.lastImage = cordova.file.dataDirectory+newFileName;
this.common.showError("Response", this.lastImage);
}, error => {
this.common.showError("Response", JSON.stringify(error));
});
}
<ion-content padding>
<img src="{{pathForImage(lastImage)}}" style="width: 100%" [hidden]="lastImage === null">
<h3 [hidden]="lastImage !== null">Please Select Image!</h3>
</ion-content>
<ion-footer>
<ion-toolbar color="primary">
<ion-buttons>
<button ion-button icon-left (click)="presentActionSheet()">
<ion-icon name="camera"></ion-icon>Select Image
</button>
<button ion-button icon-left (click)="uploadImage()" [disabled]="lastImage === null">
<ion-icon name="cloud-upload"></ion-icon>Upload
</button>
</ion-buttons>
</ion-toolbar>
</ion-footer>