Issue
i am developing a mobile application, and i want to open the gallery for select a picture, but when the button is clicked the application does not anything, not even throw an error, this is my code:
photos: any = '';
base64Image = '';
async agregarImg() {
const actionSheet = await this.actionSheetController.create({
header: 'Seleccionar medio',
buttons: [{
text: 'Cámara',
icon: 'camera',
handler: () => {
this.takePicture(CameraSource.Camera, false)
}
}, {
text: 'Galería',
icon: !this.platform.is('ios') ? 'ios-images-outline' : 'images-outline',
handler: () => {
this.openGallery();
}
}]
});
await actionSheet.present();
}
openGallery() {
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.DATA_URL,
mediaType: this.camera.MediaType.PICTURE,
encodingType: this.camera.EncodingType.JPEG,
correctOrientation: true,
allowEdit: false,
}
this.camera.getPicture(options).then((imageData) => {
this.base64Image = 'data:image/jpeg;base64,' + imageData;
this.photos.push(this.base64Image);
this.photos.reverse();
}, (err) => {
console.log(err);
})
}
I read about the problem, probably this happens for the plugin, this does not work perfectly on Android. I use this plugin: import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
Solution
Change my old code to this new one:
async agregarImg() {
const actionSheet = await this.actionSheetController.create({
header: 'Seleccionar medio',
buttons: [{
text: 'Cámara',
icon: 'camera',
handler: () => {
this.takePicture(CameraSource.Camera, false, 1)
}
}, {
text: 'Galería',
icon: !this.platform.is('ios') ? 'ios-images-outline' : 'images-outline',
handler: () => {
this.takePicture(CameraSource.Photos, false, 2)
}
}]
});
await actionSheet.present();
}
async takePicture(source: CameraSource, editable: boolean, flag: number) {
if(flag === 1) {
this.imgPerfilCamera = await Plugins.Camera.getPhoto({
quality: 50,
resultType: CameraResultType.Uri,
saveToGallery: false,
allowEditing: editable,
source,
});
} else {
this.imgPerfilCamera = await Plugins.Camera.getPhoto({
quality: 50,
resultType: CameraResultType.Uri,
saveToGallery: false,
allowEditing: editable,
source: CameraSource.Photos
});
}
}
with this plugin
import { CameraPhoto, CameraResultType, CameraSource, Plugins } from '@capacitor/core';
Add a new file on my project root called "patch-camera.js" with this code:
const fs = require("fs");
const f =
"node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/plugin/Camera.java";
fs.readFile(f, "utf8", function(err, data) {
if (err) {
return console.log(err);
}
var result = data.replace(
"String filename = contentUri.getLastPathSegment()",
'String filename = "file"'
);
fs.writeFile(f, result, "utf8", function(err) {
if (err) return console.log(err);
});
});
And add the postinstall script in my package.json
"scripts": {
"postinstall": "node patch-camera.js"
This code works.
Answered By - alangwynn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.