Ionic Camera Restarts App

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

what is your camera code ?

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) => {

//...

}

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

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

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

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

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

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

3 Likes

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

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?

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)

It worked! Awesome! Thank you.

How does it get fixed though ?

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

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

1 Like

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

1 Like

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

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

Link is not available, we should remove this comment.