使用Cordova的Android上文件的绝对路径参考

时间:2018-12-03 15:13:53

标签: android cordova phonegap-build phonegap

我有一个使用PhoneGap和Cordova的基于Web的外部应用程序。在iOS上,我可以毫无问题地引用本地文件,例如包中的图像文件。我包含了Cordova cordova-plugin-file ,并且在外部Web服务器上,我使用路径 cdvfile:// localhost / bundle / www / img / 引用了本地图像文件。例如下面的img语法:

<img src="cdvfile://localhost/bundle/www/img/news.jpg">

但是,我 似乎无法使其适用于Android 。我尝试了以下方法和其他类似方法,但没有成功。

<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">

config.xml文件中的关键项包括:

<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://*/*" />

在index.html文件中-我的内容安全政策为:

<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'">

在本地系统上的html页面上的相对文件路径引用可以正常工作,但是当我转到外部网站和WebView应用程序时,无法为Android正确引用本地主机绝对路径。我可以在iOS上完成此操作,但无法在Android上解决。

关于如何使用绝对路径访问Android上应用程序包中的本地文件的任何建议?

1 个答案:

答案 0 :(得分:0)

尝试使用以下格式的相对路径,

<img src="assets/www/img/news.jpg">

我在语言环境json上也遇到了类似的问题,它的解决方法是这样的。

以这种格式再次将资产放入 www 目录中,然后尝试进行其他全新的Android构建。 (删除 android 目录,再次添加Cordova Android平台,然后重建)