# App refresh after camera return captured image

**URL:** <https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456>\
**Category:** ionic-v1\
**Created:** [September 30, 2014, 5:06pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456 "2014-09-30T17:06:10Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jailtas](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@Jailtas](https://forum.ionicframework.com/u/Jailtas)\
**Post date:** [September 30, 2014, 5:06pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/1 "2014-09-30T17:06:11Z")

</div>

Hey guys .I know this question has been asked before here and many times on stackoverflow , But still i am struggling here.  
First of all congrats and thanks to ionic team for a wonderful HTML5 framework

Well i am working on an app where i need camera functionality. I have seen many examples and applied them but still my app is getting refreshed when i capture image and click on right button.

This is my code

```
 .factory('Camera', ['$q', function($q) {
 return {
 getPicture: function(options) {
  var q = $q.defer();

  navigator.camera.getPicture(function(result) {
    // Do any magic you need
    q.resolve(result);
  }, function(err) {
    q.reject(err);
  }, {
  quality:30 ,
  destinationType : Camera.DestinationType.FILE_URI,
  sourceType : Camera.PictureSourceType.CAMERA,
  targetWidth: 320,
  targetHeight: 320,
  saveToPhotoAlbum: false
});

  return q.promise;
 }
 }
}])

```

and in controller:

```
$scope.takePicture = function() {
Camera.getPicture().then(function(imageURI) {
  console.log(imageURI);
  $scope.mypicture = imageURI;
}, function(err) {
  console.err(err);
});

```

From my html i am calling it in as :

```
 <a class="issue_btn blue" ng-click="takePicture()"><span data-tags="photo" data-pack="default" class="ion-camera"></span></a>

```

This is all i am doing .

Also i have included following cordova plugins:

```
 camera
 file
 file-transfer

```

I am testing it on android 4.4.4  
Ram is 1GB. I don’t think memory would be an issue here.

Kindly notify me if i am doing something wrong there or help me with some solution there.

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [October 1, 2014, 7:44am UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/2 "2014-10-01T07:44:23Z")

</div>

A promise only takes 2 functions:  
success and error case.

You are trying to pass the camera options to the promise. I think you should pass the options to your getPicture method ;).

Greetz, bengtler

---

<div class="post-metadata">

**Author:** ![Jailtas](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@Jailtas](https://forum.ionicframework.com/u/Jailtas)\
**Post date:** [October 1, 2014, 9:41am UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/3 "2014-10-01T09:41:48Z")

</div>

I tried your approach too. See my updated code.

Things does not change. My app still gets restarts when camera capture photo and i click on ok.

Take a look.

Cheers.

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [October 1, 2014, 11:27am UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/4 "2014-10-01T11:27:16Z")

</div>

It was not an approach from my side.

But you asked:

> [@Jailtas](#):
>
> Kindly notify me if i am doing something wrong there

And your usage of the promise and your function was wrong.  
Did you tried remote debugging?

---

<div class="post-metadata">

**Author:** ![Jailtas](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@Jailtas](https://forum.ionicframework.com/u/Jailtas)\
**Post date:** [October 1, 2014, 2:00pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/5 "2014-10-01T14:00:12Z")

</div>

Indeed . You put me on right track. Yes i am trying remote debugging in chrome browser.

Sometime i get camera access and captured image but sometime app again get starts. What can be the issue . Is their some memory allocation issue ? if it is how can i resolve it. Any idea.

---

<div class="post-metadata">

**Author:** ![mtCoder](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mtcoder/32/104605_2.png) [@mtCoder](https://forum.ionicframework.com/u/mtCoder)\
**Post date:** [October 1, 2014, 7:41pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/6 "2014-10-01T19:41:54Z")

</div>

Jailtas - any luck with this yet? I’m also getting a similar error where my controller is reset when interacting with the camera.

---

<div class="post-metadata">

**Author:** ![Jailtas](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@Jailtas](https://forum.ionicframework.com/u/Jailtas)\
**Post date:** [October 2, 2014, 5:35am UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/7 "2014-10-02T05:35:53Z")

</div>

No luck till Now. Looking for support 😉

---

<div class="post-metadata">

**Author:** ![Jailtas](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@Jailtas](https://forum.ionicframework.com/u/Jailtas)\
**Post date:** [October 2, 2014, 6:07am UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/8 "2014-10-02T06:07:55Z")

</div>

I am using this solution. Sometime it works , sometime app gets restarts

```
 // take picture
$scope.takePicture = function() {

pictureSource=navigator.camera.PictureSourceType.CAMERA;
destinationType=navigator.camera.DestinationType.FILE_URI;
//console.log("got camera button click");
var options = {
  quality: 100,
  destinationType: destinationType,
  sourceType: pictureSource,
  encodingType: navigator.camera.EncodingType.JPEG,
  };
if (!navigator.camera)
  {
  // error handling
  return;
  }
navigator.camera.getPicture(
  function (imageURI) {
    alert("got camera success ", imageURI);
    $scope.mypicture = imageURI;
    },
  function (err) {
    alert("got camera error ", err);
    // error handling camera plugin
    },
  options);

};

```

Take a look if it can help you.

---

<div class="post-metadata">

**Author:** ![mtCoder](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mtcoder/32/104605_2.png) [@mtCoder](https://forum.ionicframework.com/u/mtCoder)\
**Post date:** [October 2, 2014, 9:39pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/9 "2014-10-02T21:39:40Z")

</div>

My code is very similar to yours. I have two buttons, one allowing interaction with the camera itself, and one allowing a user to select a photo from the photo library. Both of these cause a full app refresh. I added alerts throughout the app to test if the full app was restarting and it appears this is the case. I read before that this can be because of a memory issue where android closes out the background app when opening the camera functions to save memory, but this doesn’t seem to be the case with my app as I am getting this error in the simulator as well as on my android phone.

I have tried using navigator.camera.getPicture directly in the controller, as well as in a factory. Currently I am using the ng-cordova $cordovaCamera. Maybe ng-cordova would help in your case? anyone from the Ionic team want to help out?

---

<div class="post-metadata">

**Author:** ![mtCoder](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mtcoder/32/104605_2.png) [@mtCoder](https://forum.ionicframework.com/u/mtCoder)\
**Post date:** [October 3, 2014, 5:06pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/10 "2014-10-03T17:06:25Z")

</div>

Finally figured it out on my end. Turns out the anchor tag I was using had a href="\*" in it. Changing this to have an empty href as in the following worked for me:

```
<a href="" ng-click="fetchPhoto()">Select Photo</a>
```

---

<div class="post-metadata">

**Author:** ![bengtler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/bengtler/32/98565_2.png) [@bengtler](https://forum.ionicframework.com/u/bengtler)\
**Post date:** [October 7, 2014, 7:37am UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/11 "2014-10-07T07:37:58Z")

</div>

Do not use href attributes in angular apps!

Use ui-sref of ui-router service or only a ng-click.  
You do not need the empty href="" on this a-tag.

---

<div class="post-metadata">

**Author:** ![skyrocket777](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/skyrocket777/32/80521_2.png) [@skyrocket777](https://forum.ionicframework.com/u/skyrocket777)\
**Post date:** [June 5, 2015, 8:28pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/12 "2015-06-05T20:28:40Z")

</div>

I wonder if you get it? I had that problem and can’t fix it so far. Thank you

---

<div class="post-metadata">

**Author:** ![chan\_123](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@chan\_123](https://forum.ionicframework.com/u/chan_123)\
**Post date:** [April 26, 2016, 8:22pm UTC](https://forum.ionicframework.com/t/app-refresh-after-camera-return-captured-image/10456/13 "2016-04-26T20:22:48Z")

</div>

> <https://stackoverflow.com/questions/8368091/phonegap-camera-restarts-the-application>
