0

I have noticed many similar posts about this issue but after reading through them I don't seem to have found my answer.

I want a "view" in my JQM PhoneGap app to take a picture from camera or Photo Library and insert it into the view/screen and also save it to local storage.

When a user exits the app session and removes the application from processes running on the phone (i.e. iOS double tap home button and swipe up on application to remove its processes) - I want the data to persist so that it will be retrieved from Local Storage and the last captured image be visible when the app is opened again and user navigates to the specific view/screen. The existing image can be overwritten with a new one.

I have followed the posting by Jérôme @ Capturing and storing a picture taken with the Camera into a local database / PhoneGap / Cordova / iOS and it captures and displays the image OK. But when I remove the app from running on my iPhone and restart it, it does not retrieve the last captured image from Local Storage.

Perhaps there is something that I am missing here? Here is the code for the whole page (bear in mind this is one page of a single page app, everything is combined into 'index.html':

<div id="Ccamera-fun" data-role="page" data-title="Camera Fun Activity">
  <header data-role="header" data-theme="e" data-position="fixed">
      <a href="#" data-rel="back" data-theme="h" data-icon="arrow-l" data-direction="reverse">Back</a>
      <h1>3.5 years - 4.5 years</h1>
  </header>
  <div data-role="content" data-theme="a" class="content">
   <h1>Camera Fun</h1>
   <div id="imageContainer"></div>
    <img width="99%" id="imgProfile" src="" />
   <p>Use your photos and ask your child to describe one in detail. Use your photos as picture talks where your child can share their memories and recall events before or after the photo was taken. <strong>Together you could invent their own story.</strong></p>
    <p>Use the buttons below to take a picture with your phone or select an image from your phone&rsquo;s Photo Library. Once you have selected a photo, it will be embedded into the top of this screen.</p>

<script type="text/javascript" charset="utf-8">
// A button will call this function
//
function takePhoto() {
    sessionStorage.removeItem('imagepath');
    // Take picture using device camera and retrieve image as base64-encoded string
    navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });
}

function onPhotoDataSuccess(imageURI) { 
        // Uncomment to view the base64 encoded image data
        // console.log(imageData);

        // Get image handle
        //
        var imgProfile = document.getElementById('imgProfile');

        // Show the captured photo
        // The inline CSS rules are used to resize the image
        //
        imgProfile.src = imageURI;
        imgProfile.style.display = 'block';
        imgProfile.style.border = '2px solid #ccc';
        imgProfile.style.marginTop = '10px';
        if(sessionStorage.isprofileimage==1){
            getLocation();
        }
        movePic(imageURI);
}

// Called if something bad happens.
// 
function onFail(message) {
    alert('Failed because: ' + message);
}

function movePic(file){ 
    window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); 
} 

//Callback function when the file system uri has been resolved
function resolveOnSuccess(entry){ 
    var d = new Date();
    var n = d.getTime();
    //new file name
    var newFileName = n + ".jpg";
    var myFolderApp = "MyAppFolder";

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {      
    //The folder is created if doesn't exist
    fileSys.root.getDirectory( myFolderApp,
                    {create:true, exclusive: false},
                    function(directory) {
                        entry.moveTo(directory, newFileName,  successMove, resOnError);
                    },
                    resOnError);
                    },
    resOnError);
}

//Callback function when the file has been moved successfully - inserting the complete path
function successMove(entry) {
    //Store imagepath in session for future use
    // like to store it in database
    sessionStorage.setItem('imagepath', entry.fullPath);
}

function resOnError(error) {
    alert(error.code);
}

</script>
   <button onclick="takePhoto();" data-theme="h">Take a Picture</button>
 </p>
    <p><img src="img/DETE-footer.png" width="320" class="footer" /></p>
  </div>
</div>
Community
  • 1
  • 1
Ryan Coolwebs
  • 1,611
  • 5
  • 22
  • 44
  • well, looks like you save last image path in sessionstorage, but do you try to look at this value at application launch? Right after cordova initialization you should try to display image from sessionstorage variable – Lixas Apr 23 '14 at 08:38
  • I will give that a shot. I am making a 'single page app' (i.e. all pages in one HTML file). Is what I'm asking to do still possible? UPDATE: I tried calling the value at cordova initialization and I've got nothing. I am not sure sessionstorage is that answer, I think I need something more permanent. – Ryan Coolwebs Apr 23 '14 at 09:02
  • yes, sure it is possible. Just give me full javascript of your app and I'll try to guide you the right way – Lixas Apr 23 '14 at 09:06
  • @Lixas, as I'm using single page app, most of the code is contained within the one HTML file, which is really big. How can I send it to you? – Ryan Coolwebs Apr 23 '14 at 09:09
  • use service like http://pastebin.com/ – Lixas Apr 23 '14 at 09:12
  • ahhh got you. is JS Fiddle ok? – Ryan Coolwebs Apr 23 '14 at 09:16
  • sure, jsfiddle is perfectly fine – Lixas Apr 23 '14 at 09:18
  • Here it is in pastebin http://pastebin.com/4gE2D00a JSFiddle was too much work to separate the code... – Ryan Coolwebs Apr 23 '14 at 09:20

1 Answers1

0

based on what I have received in http://pastebin.com/4gE2D00a

You save you image path with sessionStorage.setItem('imagepath', entry.fullPath);@2320 line. To read this value you should use sessionStorage.getItem('imagepath') check what you have in this variable with alert or console.log right after cordova initialization @24

Now, if everything is right before this step, you should have path of last image. To read file from filesystem- study FileReader api

Lixas
  • 6,938
  • 2
  • 25
  • 42
  • I've put in the following code around line 24 as you suggested: function onDeviceReady() { setTimeout(function() { navigator.splashscreen.hide(); }, 2000); sessionStorage.getItem('imagepath'); alert(imagepath); } – Ryan Coolwebs Apr 23 '14 at 10:20
  • alert('path: ' + sessionStorage.getItem('imagepath')); – Lixas Apr 23 '14 at 10:29
  • I tried what you suggested but I get nothing on the alert (have not used console.log. New pastebin is at http://pastebin.com/g3XFcjRn Sorry did not see your last comment, stackoverflow updating is acting up on me tonight. – Ryan Coolwebs Apr 23 '14 at 10:36
  • it works when I put that line of code underneath 'sessionStorage.setItem('imagepath', entry.fullPath);' but not on onDeviceReady, it keeps showing up as null. Does session storage get wiped when you double tap home button and remove app from processes? I get the feeling I am chasing the wrong solution... – Ryan Coolwebs Apr 23 '14 at 10:49
  • Probably it was my mistake about sessionStorage. Note on cordova documentation states, that localStorage provides an interface to a W3C Storage interface. It allows one to save data as key-value pairs. Note: window.sessionStorage provides the same interface, but is cleared between app launches. So, please try another solution and use localStorage to save image path and try to retrieve from there – Lixas Apr 23 '14 at 11:53
  • I've found something that works well with JSON, Lawnchair. I suppose using that would be possible? Thanks for your help all the same. I will mark yours as correct answer as it was in relation to session.storage. I've had to a lot of reading on APIs. – Ryan Coolwebs Apr 23 '14 at 13:15