Android 11 WebView 打开html文件,加载时报错:ERR_ACCESS_DENIED

问题描述:

之前andorid 10上做的需求,移植到android 11上

在项目中,system/app/HTMLViewer/ 自定义的加载html的xxxActivity.java文件访问 手机系统中的

 /system/etc/ *.html 文件时, 加载时报错:ERR_ACCESS_DENIED

原因分析:

Android 11 上面 WebSettings.setAllowFileAccess(boolean allow) 方法说明:

 The default value is {@code true} for apps targeting *     

 {@linkandroid.os.Build.VERSION_CODES#Q} and below,

 and {@code false} when targeting

* {@link android.os.Build.VERSION_CODES##R} and above.

1.  在Android 10及以下版本,这个值默认是为true的,   android 11 以上默认为false。

Enables or disables file access within WebView. * Note that this enables or disables file system access only. Assets and resources are still accessible using file:///android_asset and file:///android_res.

2. 在 WebView 中启用或禁用文件访问。请注意,这仅启用或禁用文件系统访问。 assert 和 res
    仍然可以使用 file:///android_asset 和 file:///android_res 访问。

3. android 11 上面google出于安全考虑,不推荐用webview直接访问文件,如果实际项目中有需要,就需要设置为true


解决方案:

  WebSettings.setAllowFileAccess(true)  

 

你可能感兴趣的:(Android,App,webview,android,html)