You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Cordova应用在Android 13中无法访问本地图片资源的原因?

Cordova Android 13 WebView加载本地资源失败解决方案

Android 13及更高版本收紧了WebView对file://协议本地资源的访问权限,导致旧版本中使用file:///android_asset/www/...的加载方式被拦截,触发Not allowed to load local resource错误。以下是几种可行的解决办法:

1. 使用相对路径(推荐)

Cordova WebView的根目录就是www文件夹,直接使用相对路径即可,这种方式兼容性最好,新旧Android版本都能正常运行:

var redIcon = L.icon({                                
    iconUrl: 'lib/images/marker-red-small.png',                           
    iconSize: [40, 40]
});

2. 配置WebView允许本地资源访问(不推荐,有安全风险)

如果必须使用绝对路径,可以通过修改Cordova配置或自定义WebView设置来开放权限:

  • 在项目的config.xml中添加偏好设置:
<preference name="AndroidInsecureFileModeEnabled" value="true" />
  • 若自定义WebView,在Java代码中添加以下设置:
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

注意:这种方式会降低应用安全性,Google官方不建议在高版本Android中使用。

3. 使用Cordova官方cdvfile协议

借助Cordova File插件提供的cdvfile://协议来访问本地资源,需要确保已安装Cordova File插件:

var redIcon = L.icon({                                
    iconUrl: 'cdvfile://localhost/assets/www/lib/images/marker-red-small.png',                           
    iconSize: [40, 40]
});

内容的提问来源于stack exchange,提问作者Pierric

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 08:15:52