我有一个使用 PhoneGap 和 Cordova 的基于 Web 的外部应用程序。在 iOS 上,我引用本地文件没有问题,例如包中的图像文件。我包含了 Cordova cordova-plugin-file 并且在外部网络服务器上我引用了路径为 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 上访问应用程序包中的本地文件有什么建议吗?
最佳答案
尝试使用以下格式的相对路径,
<img src="assets/www/img/news.jpg">
我在语言环境 json 方面遇到了类似的问题,它是这样解决的。
再次将此格式的 Assets 放入 www 目录中,然后尝试进行另一个干净的 Android 构建。 (删除android目录,重新加入Cordova Android平台,重新构建)
关于android - Android 上使用 Cordova 的文件的绝对路径引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596562/