问题

在Android11设备上,突然无法用WebView打开本地的html文件,我的文件存储在 getDirs目录,无需请求权限也能访问的

原因

setAllowFileAccess

官网文档解释:

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.
在WebView中启用或禁用文件访问。 请注意,这仅启用或禁用文件系统访问。 asset 和 res 仍然可以使用file:///android_asset和file:///android_res访问。

将这个方法设为true就可以了。

webviewSettiings.setAllowFileAccess(true)

官方出于安全考虑,不推荐在webview访问文件地址,不过在实际开发中总是可能会有这样的需求。在Android 10(Build.VERSION_CODES.Q)及以下版本,这个值默认是为true的,不过在Android 11 (Build.VERSION_CODES.R) 这个值默认为false,因此就出现了访问问题。

android

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐