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条回答
Fickle 薄情
2楼-- · 2019-01-04 01:33

I have a Samsung Galaxy Note II. I had the same problem. I changed this in the AndroidManifest.xml and now it works on Samsung and HTC Thunderbolt

<uses-feature android:name="android.hardware.camera" android:required="false"/>

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="10"/>

<activity  android:configChanges="orientation|keyboardHidden" />
查看更多
我命由我不由天
3楼-- · 2019-01-04 01:34

When Android relaunches the app after the picture has been taken, a resume event is sent to the app. The event contains the taken photo data. So, a possible workaround involves catching the event:

document.addEventListener('resume', function(evt) {
  if(evt.action === 'resume' && evt.pendingResult) {
    var r = evt.pendingResult;
    if(r.pluginServiceName === 'Camera' && r.pluginStatus === 'OK') {
      // r.result contains file:/// url or a base64 image.
    }
  }
}, false);

Of course, you also have to restore the state of the application. For that, save your state information to eg. localStorage before opening the camera.

查看更多
甜甜的少女心
4楼-- · 2019-01-04 01:35

I am having a similar problem where even the full example provided at Camera Documentation crashes the application on an Android device (Samsung, Galaxy S). Here is a output of the log that I get:

enter image description here

查看更多
放荡不羁爱自由
5楼-- · 2019-01-04 01:38

This is actually happening when your device activities are being killed by Garbage collector , when some of your app event is trigered, your default activity goes at background hence it is being killed.

To fix this you need to go to Developer Options There you will see the option Don't Keep Activities , just uncheck that. restart your app and try camera now. it will work smoothly.

查看更多
萌系小妹纸
6楼-- · 2019-01-04 01:39

For me it helped to use cordova background plugin: https://www.npmjs.com/package/cordova-plugin-background-mode

Include into config.xml:

<gap:plugin name="cordova-plugin-background-mode" source="npm"/>

Then trigger click event to start camera plugin and send app to background:

$(document).on("click","#btn_bild_aufnehmen",function(){        
    cordova.plugins.backgroundMode.setDefaults({ text:'desc',title:'appname'});
    cordova.plugins.backgroundMode.enable();
    //do your camera stuff
});

Do not forget to disable background mode in case of success by camera plugin:

cordova.plugins.backgroundMode.disable();

To be sure also disable it onResume and enable it onPause:

function onResume(){
   window.plugin.backgroundMode.disable();
}
function onPause(){
   window.plugin.backgroundMode.enable();
}
查看更多
倾城 Initia
7楼-- · 2019-01-04 01:41

Note that if you are testing this applications directly on your Android phone, there's an option inside "Developer options" called "Don't keep activities". When this option is checked it eliminates the activity when you leave it. Specifically in the case of the camera (and other applications lunched from your activity) it will restart your activity.

Uncheck this and see if that helps.

查看更多
登录 后发表回答