PhoneGap camera restarts the application

2019-01-04 01:15发布

I'm using PhoneGap's navigator.camera.getPicture function to retrieve a photo from the device's camera on Android.

function onSuccess(imageData) {
    alert("Success!");
}

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

$(function() {
    $("button").tap(function() {
        navigator.camera.getPicture(onSuccess, onFail, { quality: 50 }); 
    });
});

When I click the button, it does start the camera, but when I click OK on the camera app after taking a photo, it restarts the application.

I tried to:

  • use different source types.
  • use different destination types.
  • reduce quality.

Any ideas?

EDIT: I also started an issue at github.

9条回答
不美不萌又怎样
2楼-- · 2019-01-04 01:44

This problem isn't actually about Phonegap. It's a common issue on native android apps too.

It occurs because when the camera is triggered, the android activity goes background (onStop state), waiting for the camera to take the picture. Then the GC comes and kills the activity to free memory before the conclusion of camera action, and when the camera is done your activity has already died. That is why the app is restarted.

It's on Android Lifecycle docs (http://developer.android.com/reference/android/app/Activity.html ):

If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.

The same occurs by acessing the media gallery or other resources that causes your activity to go background. Phonegap (now Cordova) team is already working to improve this ( https://issues.apache.org/jira/browse/CB-14 ).

We had this problem in our company and the solution was to natively develop a Phonegap plugin to use our customized camera, then our activity never go to onStop state. Follow the android API instructions on http://developer.android.com/guide/topics/media/camera.html#custom-camera and try it too.

See ya!


Edit 1:

We submited a Google Code project named Foreground Camera Plugin that fixes the problem of Android Camera restarting Phonegap applications. There is some orientation on how to use it too. Please see: http://code.google.com/p/foreground-camera-plugin/


Edit 2:

Since this problem happens with gallery too, we submited another Google Code project named Foreground Gallery Plugin that works with Cordova and fixes this issue. Please see: http://code.google.com/p/foreground-gallery-plugin/

查看更多
Summer. ? 凉城
3楼-- · 2019-01-04 01:47

Users won't want to restart their phone to free up memory, or check a box in Settings. The user-friendly way I found to return to my app's activity after leaving the camera, was to add the line:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

in CameraLauncher.java of the camera cordova plugin right before this.cordova.startActivityForResult() attempts to pass back the camera result. This makes sure that the old activity is retrieved, rather than being garbage-collected and a new instance being launched. From the android Intent API:

int FLAG_ACTIVITY_CLEAR_TOP

"If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent."

查看更多
太酷不给撩
4楼-- · 2019-01-04 01:49

I also had the same problem. I cleared all the data from the phone to increase the phone internal memory size. Then stopped all the running applications and restarted my app. This time when the capture event was called my app did not crash.

Basically this problem exists for phones with less internal memory. This will work fine in higher end phones.

Note: Also add these extra parameters to the image captured:

 navigator.camera.getPicture(onPhotoDataSuccess, onFail, {
                   quality: 20,
                   destinationType:Camera.DestinationType.DATA_URL,
                   targetWidth: 200,
                   targetHeight: 200,
                   saveToPhotoAlbum : true
 });
查看更多
登录 后发表回答