JavaScript Warrior
JavaScript Warrior

Reputation: 763

Getting device independent path to an image in Phonegap

I'm using Phonegap 3.4, and here is what I'm trying to do:

  1. Take a photo using camera
  2. Move that photo to a location on file system
  3. Get that new location's whole path - in format file:///path/to/image.jpg, so I can put that path in an img tag.

Now, I can't figure out number 3.
In step 2. I get a FileEntry object. How can I get the path in format I want from it?
I tried FileEntry's fullPath - returns an absolute path in format /path/to/image.jpg and .toURL() which returns something like cdvfile://localhost/persistent/path/to/file

I know that file:///sdcard + an absolute path does it for Android, but not for iOS.

How can I achieve this so it works across all platforms?
Or am I missing something? Is there a different way to link to images on file system?

Upvotes: 3

Views: 2921

Answers (2)

Michal
Michal

Reputation: 447

If you are using the latest version of the file plugin (org.apache.cordova.file), you can find a method called toNativeURL() on a FileEntry. Use that instead of fullPath (or instead of toURL()). That will give you the full path to file using the file:// protocol.

If this method is not on a FileEntry, update your file plugin to the latest version.

Upvotes: 2

csantanapr
csantanapr

Reputation: 5022

If you have an file entry you can use the method .toURL()

Or if you want he uri at the time of taking picture the set destinationType: Camera.DestinationType.FILE_URI

Here is the example from cordova docs:

navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI });

function onSuccess(imageURI) { var image = document.getElementById('myImage'); image.src = imageURI; }

function onFail(message) { alert('Failed because: ' + message); }

For more info on camera and file plugin Apis check the docs for Apis http://cordova.apache.org/docs/en/3.4.0/cordova_plugins_pluginapis.md.html#Plugin%20APIs

Upvotes: -1

Related Questions