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

Android Pie测试设备WebView出现ERR_ACCESS_DENIED问题求助

解决WebView加载URL时的ERR_ACCESS_DENIED错误

Hey there, let's fix that frustrating ERR_ACCESS_DENIED error you're hitting with WebView. I’ve gone through your current AndroidManifest.xml and made a few key adjustments to address this issue, along with some extra tips to cover all bases.

修正后的完整AndroidManifest.xml配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pgm5">

    <!-- 核心权限声明 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="29" /> <!-- 针对Android 10+,限制旧存储权限的作用范围 -->

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true"
        android:requestLegacyExternalStorage="true"> <!-- 适配Android 10的Scoped Storage规则 -->

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

关键修改说明

  • android:requestLegacyExternalStorage="true":Android 10(API 29)引入了Scoped Storage,这个属性让你的应用可以暂时使用旧的存储访问逻辑,避免因权限限制导致无法加载本地文件。
  • android:maxSdkVersion="29":给WRITE_EXTERNAL_STORAGE权限加上这个限制,因为Android 11+推荐使用Scoped Storage API,不需要这个旧权限了。
  • 动态权限申请:别忘了从Android 6.0(API 23)开始,存储权限属于危险权限,必须在代码中动态申请,仅在Manifest声明是不够的。示例代码:
    private static final int REQUEST_STORAGE_PERMISSION = 100;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // 检查并申请存储权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQUEST_STORAGE_PERMISSION);
        }
    }
    

WebView代码补充配置

如果是加载本地file://协议的资源,还要确保WebView允许文件访问:

WebView webView = findViewById(R.id.your_webview_id);
WebSettings webSettings = webView.getSettings();
webSettings.setAllowFileAccess(true);
webSettings.setAllowFileAccessFromFileURLs(true);
// 如果需要允许跨文件访问,可添加下面这行(根据需求选择)
webSettings.setAllowUniversalAccessFromFileURLs(true);

这些调整应该能解决你的ERR_ACCESS_DENIED问题了。如果还是不行,检查一下你加载的URL是否合法,或者有没有其他安全限制(比如混合内容拦截,但你已经开了usesCleartextTraffic,应该没问题)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:01:35