Hello all,
I did not needed any help for a long time but today I need one. I am building an app with ionic 5 and cordova. Camera plugin installed. On android devices after taking photo by camera is everything fine, but on one device with android 10 app crashes after I submit taken photo. Selecting photo from gallery works well.
my camera options:
const cameraOptions: CameraOptions = {
quality: 50,
targetWidth: 500,
targetHeight: 700,
sourceType: source,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
correctOrientation: true
};
const imagePath = await this.camera.getPicture(cameraOptions);
this.convertedFile = await this.fileLoaderSrv.getSingleFile(imagePath);
await this.fileLoaderSrv.convertFileSrc(imagePath);
and handling taken photo:
public async getSingleFile(filePath: string): Promise<File> {
// Get FileEntry from image path
const fileEntry: FileEntry = (await this.ionicFileSrv.resolveLocalFilesystemUrl(filePath)) as FileEntry;
// Get File from FileEntry. Again note that this file does not contain the actual file data yet.
const cordovaFile: IFile = await this.convertFileEntryToCordovaFile(fileEntry);
// Use FileReader on each object to populate it with the true file contents.
return this.convertCordovaFileToJavascriptFile(cordovaFile);
}
private convertFileEntryToCordovaFile(fileEntry: FileEntry): Promise<IFile> {
return new Promise<IFile>((resolve, reject) => {
fileEntry.file(resolve, reject);
});
}
public convertCordovaFileToJavascriptFile(cordovaFile: IFile): Promise<File> {
return new Promise<File>((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => {
if (reader.error) {
reject(reader.error);
} else {
const blob: any = new Blob([reader.result], { type: cordovaFile.type });
blob.lastModifiedDate = new Date();
blob.name = cordovaFile.name;
resolve(blob as File);
}
};
reader.readAsArrayBuffer(cordovaFile);
});
}
public async convertFileSrc(imagePath): Promise<any> {
// Extract just the filename. Result example: cdv_photo_003.jpg
let tempFilename = imagePath.substr(imagePath.lastIndexOf('/') + 1);
if (tempFilename.includes('?')) {
tempFilename = tempFilename.split('?')[0];
}
// Now, the opposite. Extract the full path, minus filename.
// Result example: file:///var/mobile/Containers/Data/Application
// /E4A79B4A-E5CB-4E0C-A7D9-0603ECD48690/tmp/
const tempBaseFilesystemPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
// Get the Data directory on the device.
// Result example: file:///var/mobile/Containers/Data/Application
// /E4A79B4A-E5CB-4E0C-A7D9-0603ECD48690/Library/NoCloud/
const newBaseFilesystemPath = this.ionicFileSrv.dataDirectory;
await this.ionicFileSrv.copyFile(tempBaseFilesystemPath, tempFilename, newBaseFilesystemPath, tempFilename);
const storedPhoto = newBaseFilesystemPath + tempFilename;
const resolvedImg = this.webview.convertFileSrc(storedPhoto);
return this.domSanitizer.bypassSecurityTrustUrl(resolvedImg);
}
Any help appriciated.
Thanks a lot