12

Is there a way to get the absolute path of an image in phonegap asset folder?

I need the absolute path to attach the image in a mail using EmailComposer plugin.

EmailComposer plugin wants an absolute path like this:

 /storage/sdcard0/DCIM/Camera/20121124_125210.jpg

But the image I want to attach is in my assets folder and a path like this:

file:///android_asset/www/img/logo.png

does not work

Cesar
  • 4,076
  • 8
  • 44
  • 68

3 Answers3

12

Just in case somebody is still looking for a solution. Now Cordova implements a really easy way to get the current application directory (supported on Android, iOS and BlackBerry 10) :

cordova.file.applicationDirectory

For example, if you want to access an audio file in your res/audio directory (within your www), you just have to write that :

cordova.file.applicationDirectory + 'res/audio/my-audio-file.mp3'

Here is the complete documentation

Antonino
  • 3,178
  • 3
  • 24
  • 39
Ivan Gabriele
  • 6,433
  • 5
  • 39
  • 60
2

It looks like this is not possible.

See https://groups.google.com/forum/#!topic/phonegap/Sw2wThOYm8c

From Simon's response in that thread he states...

"You can't do what you want to do. The files in the assets directory are not technically on the file system so they are not accessible via the File API. This means calling window. resolveLocalFileSystemURI() will not return you a FileEntry. It is properly returning an error code of 1 which is NOT_FOUND_ERR. "

You need the absolute path of a file for the email attachments plugin to work. Unfortunately if the file resides in the assets folder, you cannot use cordova's file api to get the absolute path.

There is a plugin that exists to copy files out of your assets folder and place them onto the sdcard. You can then pass the exact location of this file to the email attachments plugin. Unfortunately it looks like the plugin below is out of date if you are planning on using the newer cordova versions.

https://github.com/gkcgautam/Asset2SD

njtman
  • 2,160
  • 1
  • 19
  • 32
  • 1
    I can't comment on other people's answers yet because I don't have enough points, but wraith's answer is incorrect. See my answer above as an explanation as to why it is incorrect. It possibly may have worked on older versions of android and phonegap, but it definitely doesn't work on cordova/phonegap 2.0.0 +. This method will not work for files stored within the apk unfortunately. – njtman Apr 20 '13 at 16:22
0

Pass 'file:///android_asset/www/img/logo.png' to window.resolveLocalFileSystemURI() and the resulting FileEntry.fullPath is what you want. Look at the documentation for detailed example: http://docs.phonegap.com/en/1.5.0/phonegap_file_file.md.html

wraith
  • 391
  • 1
  • 14