Image File URI permanent or temp?

3,822 views
Skip to first unread message

Robert Stewart

unread,
Jun 10, 2011, 4:50:13 AM6/10/11
to phonegap
Hi,
Quick question: Grabbing an image and returning the image path via
'destinationType : 1'

on the iPhone this gives: file://localhost/var/mobile/Applications/..../Documents/tmp/photo_002.jpg.

Is this a permanent location or temp (as the link implies)? i.e. could
I stick this location in an SQL database (instead of base64 image) and
call it back at a later time to dispaly the image or is this temp data
which is deleted everytime the App is terminated and restarted?

Any help much appreciated.

regards,
Robert

Becka11y

unread,
Jun 10, 2011, 2:33:07 PM6/10/11
to phonegap

The location is temporary - that tmp dir is deleted when the
application ends. You would need to copy the file using the File
apis to the persistent store.

-becky

Robert Stewart

unread,
Jun 13, 2011, 3:58:55 AM6/13/11
to phonegap
Hi Becky,
Thanks for this very useful info and thought that may be the case.
Don't suppose you have an example script or few lines of code? I've
looked at the API docs and couldn't get anything working for the
iphone.

Regards,
Robert

Becka11y

unread,
Jun 16, 2011, 5:29:25 PM6/16/11
to phonegap
Here is an example that worked for me AFTER I made one fix to file.m
in the PhoneGapLib framework. Thus, this will likely not work in
0.9.5.1 - the window.resolveLocalFileSystemURI will fail - I think -
still testing. Will update with more info but wanted to capture this
much.

function getAndSavePhoto(){
navigator.camera.getPicture(photoSave, onFail, { quality:
50,
destinationType:
destinationType.FILE_URI,
sourceType:
pictureSource.SAVEDPHOTOALBUM });

}

function photoSave(imageURI) {
// this relies on knowledge of photo file URI - you might
want to make this more robust :-)
var imgFileName = imageURI.substr(imageURI.lastIndexOf('/')
+1);
var imgPath = "tmp/" + imgFileName;
console.log(imgFileName);


var gotFileEntry = function(fileEntry) {
console.log("got image file entry: " +
fileEntry.fullPath);
var gotFileSystem = function(fileSystem){
// copy the file
fileEntry.copyTo(fileSystem.root, null,
copiedFile, fsFail);
};
// get file system to copy or move image file to
window.requestFileSystem(LocalFileSystem.PERSISTENT,
0, gotFileSystem, fsFail);
};
//resolve file system for image
//(image is currently stored in dir off of persistent file
system but that may change)
window.resolveLocalFileSystemURI(imageURI, gotFileEntry,
fsFail);
}

function copiedFile(fileEntry) {
console.log("copied file: " + fileEntry.fullPath);
// !!! assumes you have an img element on page with
id=largeImage
var largeImage = document.getElementById('largeImage');

largeImage.style.display = 'block';
largeImage.src = fileEntry.toURI() + "?" + (new
Date()).getTime();
}

// file system fail
function fsFail(error) {
console.log("failed with error code: " + error.code);
};

// camera fail
function onFail(message) {
alert('Failed because: ' + message);

Robert Stewart

unread,
Jun 30, 2011, 10:25:16 AM6/30/11
to phonegap
Hi becky,
Sorry for the late reply of this but I've just seen the code today and
it's exactly what I needed!!! I've worked through it and got it all
working so many thanks as I've been struggling on this for a while.
However, not sure why you added date and time to the image src? i.e.

largeImage.src = fileEntry.toURI() + "?" + (new Date()).getTime()

Why did you do this? why not just:

largeImage.src = fileEntry.toURI();

Regards,
Robert

On Jun 16, 10:29 pm, Becka11y <gibson.be...@gmail.com> wrote:
> Here is an example that worked for me AFTER I made one fix to file.m
> in the PhoneGapLib framework. Thus, this will likely not work in
> 0.9.5.1 - the  window.resolveLocalFileSystemURI will fail - I think -
> still testing.  Will update with more info but wanted to capture this
> much.
>
> function getAndSavePhoto(){
>             navigator.camera.getPicture(photoSave, onFail, { quality:
> 50,
>                                         destinationType:
> destinationType.FILE_URI,
>                                         sourceType:
> pictureSource.SAVEDPHOTOALBUM });
>
>         }
>
> function photoSave(imageURI) {
>             // this relies on knowledge of photo file URI - you might
> want to make this more robust :-)
>             var imgFileName = imageURI.substr(imageURI.lastIndexOf('/')
> +1);
>             var imgPath = "tmp/" + imgFileName;
>             console.log(imgFileName);
>
>             var gotFileEntry = function(fileEntry) {
>                 console.log("gotimagefile entry: " +
> fileEntry.fullPath);
>                 var gotFileSystem = function(fileSystem){
>                     // copy the file
>                     fileEntry.copyTo(fileSystem.root, null,
> copiedFile, fsFail);
>                };
>                 // get file system to copy ormoveimagefile to
>                 window.requestFileSystem(LocalFileSystem.PERSISTENT,
> 0, gotFileSystem, fsFail);
>             };
>             //resolve file system forimage
>             //(imageis currently stored in dir off of persistent file
> > > > Quick question: Grabbing animageand returning theimagepath via
> > > > 'destinationType : 1'
>
> > > > on the iPhone this gives: file://localhost/var/mobile/Applications/..../Documents/tmp/photo_002.jpg.
>
> > > > Is this a permanent location or temp (as the link implies)? i.e. could
> > > > I stick this location in an SQL database (instead of base64image) and
> > > > call it back at a later time to dispaly theimageor is this temp data

Becka11y

unread,
Jun 30, 2011, 4:55:49 PM6/30/11
to phonegap
I add the date as a uri parameter so the source parameter changes and
the webview will reload the image. Sometimes when I test I
immediately delete the image so when I request a new image I might get
the same file name back (based on how I generate the file names in the
obj. c code). If I didn't do this, the webview would see no change in
the src value and the image would not be reloaded. Just a habit I
got into, it is not required unless you have similar situation where
the file name may always come back the same.


FYI - in 0.9.6 I modified the getPicture code so that the file is now
stored in the apps default temporary directory:
LocalFileSystem.TEMPORARY. That changes this example because the
window.resolveLocalFileSystemURI(imageURI, gotFileEntry,
fsFail); will NOW return the TEMPORARY fileSystem not the PERSISTENT
one as it was before. You'll have to do a bit of rework to get the
PERSISTENT file system to copy to.

-becky

Robert Stewart

unread,
Jul 1, 2011, 4:19:58 AM7/1/11
to phonegap
Thanks for the info, makes sense now. I'll have a look at a work
around for 0.9.6. Thanks for all your help on this.


Regards,
Robert

On Jun 30, 9:55 pm, Becka11y <gibson.be...@gmail.com> wrote:
> I add the date as a uri parameter so the source parameter changes and
> the webview will reload theimage.  Sometimes when I test I
> immediately delete theimageso when I request a newimageI might get
> the same file name back (based on how I generate the file names in the
> obj. c code). If I didn't do this, the webview would see no change in
> the src value and theimagewould not be reloaded.   Just a habit I
> got into, it is not required unless you have similar situation where
> the file name may always come back the same.
>
> FYI - in 0.9.6 I modified the getPicture code so that the file is now
> stored in the apps default temporary directory:
> LocalFileSystem.TEMPORARY.  That changes this example because the
> window.resolveLocalFileSystemURI(imageURI, gotFileEntry,
> fsFail);  will NOW return the TEMPORARY fileSystem not the PERSISTENT
> one as it was before.  You'll have to do a bit of rework to get the
> PERSISTENT file system to copy to.
>
> -becky
>
> On Jun 30, 10:25 am, Robert Stewart <robert.stew...@ed.ac.uk> wrote:
>
>
>
>
>
>
>
> > Hi becky,
> > Sorry for the late reply of this but I've just seen the code today and
> > it's exactly what I needed!!! I've worked through it and got it all
> > working so many thanks as I've been struggling on this for a while.
> > However, not sure why you added date and time to theimagesrc? i.e.

Satheesh kumar

unread,
Mar 27, 2012, 6:04:12 AM3/27/12
to phon...@googlegroups.com
Thank You Robert Stewart,

Actually my issue is not related to this question. But your code helped me to fix my issue. You saved my day. Thank You.

Mani Babu Ankireddy

unread,
May 29, 2015, 3:05:36 PM5/29/15
to phon...@googlegroups.com
Hi,
Becka11y 
 im getting the issue in my phonegap application im using cordova camera plugin , when im capturing the image my application is restarting i dont know why it is happening ....i gone searched many days till i didnt  get result. so please find the solution to my question bcoz my project is in final stage so please im requesting you
here is my code..........

function capturePhoto() {

      clearCache();

     var today = new Date();

var dd = today.getDate();

var mm = today.getMonth()+1;//January is 0!

var yyyy = today.getFullYear();

var hours = today.getHours();

var minutes = today.getMinutes();

var seconds = today.getSeconds();

if(dd<10){dd='0'+dd}

if(mm<10){mm='0'+mm}

if(minutes<10){minutes='0'+minutes}

if(seconds<10){seconds='0'+seconds}

var post_todaydate  = mm+'/'+dd+'/'+yyyy+' '+hours+':'+minutes+':'+seconds

document.getElementById('timedataid').innerHTML = post_todaydate;

        document.getElementById("submitbtndaily").style.display = '';

        document.getElementById("timedataid").style.display = '';

        document.getElementById("largeImage").style.display = '';

        navigator.camera.getPicture(onCapturePhoto, onFailure, {

                                    quality:10,

                                    destinationType: destinationType.FILE_URI

                                    });

    }

    

    function clearCache() {

    //alert("hi");

    navigator.camera.cleanup();

}

 

    

    <!---------gallery image--------------------------->

    function getPhoto(source)

    {

    clearCache();

       var today = new Date();

var dd = today.getDate();

var mm = today.getMonth()+1;//January is 0!

var yyyy = today.getFullYear();

var hours = today.getHours();

var minutes = today.getMinutes();

var seconds = today.getSeconds();

if(dd<10){dd='0'+dd}

if(mm<10){mm='0'+mm}

if(minutes<10){minutes='0'+minutes}

if(seconds<10){seconds='0'+seconds}

var post_todaydate  = mm+'/'+dd+'/'+yyyy+' '+hours+':'+minutes+':'+seconds

document.getElementById('timedataid').innerHTML = post_todaydate;

        document.getElementById("submitbtndaily").style.display = '';

        document.getElementById("timedataid").style.display = '';

        document.getElementById("largeImage").style.display = '';

        navigator.camera.getPicture(onCapturePhoto, onFailure, { quality: 10,

                                    destinationType: destinationType.FILE_URI,

                                    sourceType: source });

    }

    <!--------------upload image failed------------------>

    function onFailure(message) {

    

    $('#largeImage').attr('src', '');

        //alert('Failed because: ' + message);

        $("#camphoto").popup( "close" );

        $("body").off("touchmove");

        

        

    }

    

    //  <!----------giving image name,key called from capturephoto/getphoto-------------------->

    function onCapturePhoto(fileURI) {

       $('#largeImage').attr('src', '');

        myfileURI = fileURI;

        clearCache();

        var largeImage = document.getElementById('largeImage');

        largeImage.style.display = 'block';

        largeImage.src = fileURI;

        options = new FileUploadOptions();

        options.fileKey = "file";

        options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);

        options.mimeType = "image/jpeg";

Jesse Monroy

unread,
May 29, 2015, 11:13:50 PM5/29/15
to phon...@googlegroups.com
@Mani,
This post is over 3 years old. It is unlikely anyone will answer. Please start a new thread and state your problem.
TIA
Jesse

Mani Babu Ankireddy

unread,
May 30, 2015, 7:32:48 AM5/30/15
to phon...@googlegroups.com
@jessey 
ya! my application is clashing  in samsung galaxy s4, android version-5.0.1 when capturing a image through camera..i dont know why it is happening like that,so please give me the solution as early as possible.

--
-- You received this message because you are subscribed to the Google
Groups "phonegap" group.
To post to this group, send email to phon...@googlegroups.com
To unsubscribe from this group, send email to
phonegap+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/phonegap?hl=en?hl=en
 
For more info on PhoneGap or to download the code go to www.phonegap.com
---
You received this message because you are subscribed to a topic in the Google Groups "phonegap" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phonegap/xHayIk3dIAM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phonegap+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages