Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get absolute path to assets folder in PhoneGap

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

like image 585
Cesar Avatar asked Mar 07 '13 01:03

Cesar


2 Answers

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

like image 189
Ivan Gabriele Avatar answered Oct 24 '22 00:10

Ivan Gabriele


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

like image 41
njtman Avatar answered Oct 24 '22 02:10

njtman