9

I am developing phonegap app. And using camera in it. It was working few days ago. I did many changes in last few days and now camera is not working correctly. It opens camera but when I take photo , then from camera app. when I press OK, it restart app.

I am not understanding how it is doing so. I actually unable to understand whether it can restart from my phonegap app. JS code or it only can be restart from android Java code.

I am using this code for camera:

function takePhoto() {
navigator.camera.getPicture(onCameraPicSuccess, onCameraPicFail, {quality: 50,
    destinationType: Camera.DestinationType.FILE_URI
});

function onCameraPicSuccess(imageURI) {
    alert("coming here in pic success");
    var img = '<div style="padding:4px; margin:10px;  border: solid 2px #666;float: left; width:30%"><img src="' + imageURI + '" width="100%" /></div>';
    $('#images_area').append(img);
 }

function onCameraPicFail(message) {
    alert('Failed because: ' + message);
}

So any idea, that why app. will restart on using camera app. through above code? My JS code don't have splash screen but my Java code have that. So after pressing OK button of camera, I can see app. restarted with back button binding and menu button binding not working for a while.

So not sure what is happening. If you guys have any such experience or any idea then feel free to share.

If there is some thing more I need to tell. Then let me know.


After doing some more search on stackoverflow.com, I came to know that issue is of android, as android kill the app. in background if it have less memory. So need to free the memory e.t.c. But what I noticed is that application was working fine when I was using JQuery Mobile Forms. But after I changed it to custom, this camera problem appear. So if this is memory related problem, then why it occured on removing jquery mobile forms and doing custom work. In fact, now application is even more smooth with custom work. So I am confused that whether it is memory related problem or something else? And how to tackle this type of app. developed in phonegap.

Hafiz
  • 4,187
  • 12
  • 58
  • 111

5 Answers5

4

The phonegap camera API invokes the camera application when you use

navigator.camera.getPicture

Please read it here - http://docs.phonegap.com/en/edge/cordova_camera_camera.md.html#Camera

This pushes your application into the background and that can lead to your app being killed if there isn't enough memory.

If you don't want your application to go into background while the camera is up, you need to use a foreground camera plugin. Take a look at it here

You might need to change it to suit your requirements.

UnTechie
  • 365
  • 2
  • 7
  • but that way I will probably need the plugin for iOS too? – Hafiz Nov 12 '13 at 02:54
  • +1 for showing me another way, but couldn't test it yet so I will let you know after trying you way – Hafiz Nov 12 '13 at 19:44
  • unable to run this plugin, after including it into project as told in its project page, unable to compile and make build – Hafiz Nov 12 '13 at 20:27
  • it says duplicate class ForegroundCameraPreview, is it because of already installed cordova camera plugin? – Hafiz Nov 12 '13 at 20:54
  • I'm using phonegap 2.9 + foregroundcameraplugin, when going to the camera the preview is black screen, clicking 'Capture' kill the app with NullPointerException (CameraActivity.java:77) – Elia Weiss Apr 27 '14 at 15:54
  • Also that plugin is just for Android – Dani Feb 13 '18 at 13:25
1

It can be a memory related problem. You can increase the heap size of your application by setting android:largeHeap="true" in your application manifest file.

Vicky Gonsalves
  • 11,593
  • 2
  • 37
  • 58
  • no difference, on using camera, it is having same problem, restarting the app. on pressing OK button after capturing image. – Hafiz Nov 09 '13 at 04:16
1

You shouldn't need to create a whole custom foreground camera, you just need to specify to the aggressive garbage collector that you want to retrieve the old intent instead of starting a new instance. Try setting a flag on the saved intent within the camera plugin:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

See my post here: https://stackoverflow.com/a/29630548/2782404

Furthermore, increasing heap size and using singleTask/singleInstance didn't work for me

Community
  • 1
  • 1
kwills
  • 116
  • 10
0

In android menifest file set the launchmode of the activity to "singleTask" or "singleInstance".
May be your appliction launching the activity in multiple time.

Rawat Raman
  • 479
  • 1
  • 5
  • 13
0

see this link, it explains that this does not actually have to do with phonegap, but with the activities of android. He suggests using a plugin called Foreground Camera Plugin.

PhoneGap camera restarts the application

Community
  • 1
  • 1