Ionic Camera Restarts App


#1

I have been working with this issue for about a month now, and cannot find a solution.

On my Android device, whenever I attempt to take/choose a picture using cordova-plugin-camera, the app will crash and restart itself. I have tried updating the plugins, downgrading to different SDK versions, and even tried another camera plugin, but none of them are working. If you know of any solutions, I would appreciate the help.

  • Brad

#2

what is your camera code ?


#3

I’m having the same issue. Both Camera and Gallery restart the app:

let options:CameraOptions = {
      quality: 100,
      cameraDirection: this.camera.Direction.FRONT,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true,
      targetHeight: 640,
      targetWidth: 640,
      allowEdit: false,
      saveToPhotoAlbum: false
    }

//...

options.sourceType = this.camera.PictureSourceType.CAMERA;
this.camera.getPicture(options).then((imageData:string) => {
  this.imageCropperModal(imageData);
}, (err:string) => {

//...

}

#4

It has nothing to do with the code. The problem is described here: https://cordova.apache.org/docs/en/latest/guide/platforms/android/#lifecycle-guide


#5

I posted some workarounds here, hope this helps (Ionic 2 code).


#6

Still the same problem. I tried with --prod.


#7

Hi, I’m pretty sure you can workaround this issue, for example using a dedicated server for images, or tweaking the code so it doesn’t wait for local answer for uploading. Or using Lazy Load. In any case my code works perfect with no change. I don’t pretend it’s better, just that it prevents any kind of timeout.

Hope it helps,

Francois


#8

@FrancoisIonic - thanks, I’ll keep that in mind!


#9

I had the same problem. To resolve this I decreases 3 parameters in CameraOptions:
quality, targetWidth and targetHeight


#10

I have the same problem and the solution was remove target width and heigh.
Follow @mferrari sugestion.


#11

Below are my camera options:

options: CameraOptions = {
    quality: 10,// still takes a long time, and usually crashes
    destinationType: this.camera.DestinationType.DATA_URL,
    encodingType: this.camera.EncodingType.PNG,
    mediaType: this.camera.MediaType.PICTURE,
    allowEdit: false, //change to true later
    saveToPhotoAlbum: true
  }

I think the issue is that it is computationally expensive to create a Base64 URL from an image and therefore it will be easier to simply use FileTransfer. Do you think that is correct?


#12

I think not… try change PNG to JPEG!

My code:

openCamera() {
		if (this.platform.is('cordova')) {
			const options: CameraOptions = {
				quality: 100,
				targetHeight: 400,
				targetWidth: 400,
				destinationType: this.camera.DestinationType.DATA_URL,
				encodingType: this.camera.EncodingType.JPEG,
				mediaType: this.camera.MediaType.PICTURE,
				correctOrientation: true,
				allowEdit: true,
			}

			this.camera.getPicture(options).then((imageData) => {
				let base64Image = 'data:image/jpeg;base64,' + imageData;
				this.userPicture = base64Image;
			}).catch((err) => {
				console.log("Error: ", err);
			});

		} else {
			this.userPicture = 'https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png';
		}

	}

	openGallery() {
		if (this.platform.is('cordova')) {
			const options: CameraOptions = {
				quality: 100,
				targetHeight: 400,
				targetWidth: 400,
				destinationType: this.camera.DestinationType.DATA_URL,
				encodingType: this.camera.EncodingType.JPEG,
				mediaType: this.camera.MediaType.PICTURE,
				sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
				allowEdit: true
			}

			this.camera.getPicture(options).then((imageData) => {
				let base64Image = 'data:image/jpeg;base64,' + imageData;
				this.userPicture = base64Image;
			}).catch((err) => {
				console.log("Error: ", err);
			});
		} else {
			this.userPicture = 'https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png';
		}
	}

If return error, can you share here?

(sorry my bad English)


#13

It worked! Awesome! Thank you.


#14

How does it get fixed though ?


#15

There you go. I documented all my analysis and solution. Also the last post is a link to a demo project.

https://forum.ionicframework.com/t/solved-camera-plugin-restart-app-on-android-8-1

#16

@reedrichards, I am taking a look at this. Hopefully I do not hit any walls


#17

It’s not a wall, but if you want to handle it, it’s a bit of mambo jambo


#18

its memory issue ,your device cannot save your image then it will crush


#19

use “cordova-plugin-background-mode”
Link: https://ionicframework.com/docs/native/background-mode/
https://github.com/katzer/cordova-plugin-background-mode

this plugin can prevent the app from going to sleep while in background.
so it not be killed by the system