Tom Söderlund
Tom Söderlund

Reputation: 4747

Cordova: dynamically loading a local image fails on Android/iOS

In my Cordova app, I’m loading images dynamically from the local /www/img folder:

var imageURL = 'img/' + imageFilename;
element.css('background-image', "url('" + imageURL + "')");

It works fine in browser (cordova run browser), but fails for both Android and iOS (emulator + device).

What’s wrong? What should local URLs for Cordova look like?

I’ve also tried with:

var imageURL = 'file:///img/' + imageFilename;

enter image description here

Upvotes: 2

Views: 2916

Answers (1)

palucdev
palucdev

Reputation: 316

I think its the common problem with file locations across mobile platforms.
To navigate into app directory, you must use localizations from cordova file plugin.
Like cordova.file.applicationStorageDirectory which maps /var/mobile/Applications/<UUID>/ on iOS and file:///android_asset/ on Android. Then you can easily create path to your resource like: cordova.file.applicationStorageDirectory + "/yourDir/yourfile".
For more information about cordova file paths, visit https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/

Upvotes: 1

Related Questions