How do you reference Absolute Path to files on Android with Cordova and WebView app

349 views
Skip to first unread message

Stephen Smith

unread,
Dec 3, 2018, 11:27:02 AM12/3/18
to phonegap
I have an external Web based app using PhoneGap and Cordova.  On iOS, I have no problem referencing local files, such as image files in the package.  I include the Cordova cordova-plugin-file and on the external web server I successfully reference the local image files for iOS with the path cdvfile://localhost/bundle/www/img/ such as the img syntax below:
   
<img src="cdvfile://localhost/bundle/www/img/news.jpg">

But, I cannot seem to make it work for an Android.  I have tried the following methods and other similar paths, but no success.

    <img src="file://localhost/assets/www/img/news.jpg">
   
<img src="cdvfile://localhost/assets/www/img/news.jpg">
   
<img src="file:///localhost/assets/www/img/news.jpg">
   
<img src="file:///android_assets/www/img/news.jpg">

Key items in my config.xml file include:
    <plugin name="cordova-plugin-whitelist" source="npm"/>
   
<plugin name="cordova-plugin-file" source="npm"/>
   
<access origin="*" />
   
<allow-navigation href="*" />
   
<allow-intent href="http://*/*" />
   
<allow-intent href="https://*/*" />


In the index.html file - my Content Security Policy is:

    <meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src * 'unsafe-inline' 'unsafe-eval'">

Relative file path references on the html pages on the local system work fine, but when I go to the external web site and WebView app I can't reference the localhost absolute path correctly for an Android.  I can do it on iOS, but can't figure it out for Android.

Any suggestions on how to access local files in the app package on an Android with an absolute path?
Reply all
Reply to author
Forward
0 new messages