Recover camera captured image after app was restarted (by the camera itself)

I’m having a pretty common issue on Ionic with Android (It’s also
common in Cordova/PhoneGap) where on low ram devices, when you call the
camera to get a picture (or any media intent actually), your app goes
background and is often closed by the Garbage Collector, and then
restarted after the picture was taken.
This is the code I’m using to call the camera, with the ngCordova Camera
plugin:

var options = {
   quality: 75,
   destinationType: Camera.DestinationType.FILE_URI,
   //destinationType: Camera.DestinationType.DATA_URL,
   sourceType: Camera.PictureSourceType.CAMERA,
   allowEdit: true,
   encodingType: Camera.EncodingType.JPEG,
   targetWidth: 200,
   targetHeight: 200,
   popoverOptions: CameraPopoverOptions,
   saveToPhotoAlbum: false
};

// This causes the app to go background
$cordovaCamera.getPicture(options).then(function (imageData) {
    // do something with the picture
}, function (err) {
    // do something with the error
});

I searched far and wide and there seem to be just 2 posible solutions:

Implementing your own inApp camera, so your app never goes
background.Saving your app state and restoring it after taking the
picture.

For multiple reasons, I’m going for the later, saving and restoring
my app state, which of course includes retrieving the picture data the
camera is returning. After this long context desciption, this is my
actual question:

How can I retrieve the imageData the camera is returning to my app?

I might be lacking some basic Android knowledge, but I believe the
Camera app is returning an Intent to my app, so I tried to recover it
with this WebIntent plugin, with no success at all.

Problem solved, in case anyone is having the same issue, this is what I added in the app.run method:

  document.addEventListener('resume', function(evt) {
      if(evt.action === 'resume' && evt.pendingResult) {

            var r = evt.pendingResult;
            if(r.pluginServiceName === 'Camera' && r.pluginStatus === 'OK') {
                var restoredImageData = r.result;
            }
      }
    }, false);
2 Likes

Sorry to reopen an old discussion,

but is there a way to do this in Angular 2 (Ionic 2):

I tried to adapt your code as follows
//on app resume
platform.resume.subscribe(e => {
if(e.action === ‘resume’ && e.pendingResult) {
var r = e.pendingResult;
if(r.pluginServiceName === ‘MediaCapture’ && r.pluginStatus === ‘OK’) {
var data = r.result;
var path = data[0].fullPath;
path = path.replace(‘file:/’,‘file:///’);
var params = {type:‘img’, path: path};
//send it to be confirmed
this.navCtrl.push(ConfirmPage, params)
}
}
});

but this seems to be doing nothing.
Do you know a workaround?
Thank you again

1 Like

I haven’t worked with Ionic2 yet so I don’t really know, I also didn’t need this feature at all in the end so I didn’t keep looking on it. Hope somebody can help you.

@valentinoa did you find a fix?

@shashank123 no sorry I didnt :confused: most new cellphones though dont have the issue

@valentinoa - Because they have better memories, I assume? Will it work automatically, right off the bat?

Yes and yes (at least that has been my experience so far :slight_smile: )

Good morning… Same problem and it’s very annoying!!! Especially because it looks like app’s bug!!