自 Android 11 (SDK30) 以来,我无法从应用程序特定存储将 HTML 文件加载到 WebView

I cant load HTML file into WebView from app specific storage since Android 11 (SDK30)

我的应用有以下用例:

我们针对用户选择的特定语言从服务器下载一个 geojson 文件、一个 html 文件和一些音频文件,并将其保存在应用特定的文件目录中。

每个用例的目录路径如下:

对于 HTML 文件,我们有:

/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/EnginePayload/de/html-file.html

对于 json 文件,我们有:

/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/LocationPayload/de/geojson-file.geojson

音频数据:

/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/Audio/de/intro.mp3
/storage/emulated/0/Android/data/my.example.com/files/ExampleApp/Audio/de/outro.mp3

等等...

因为我必须将 minSDK 和 compileSDK 更改为 30,所以我无法使文件正常工作。 我将 html-file.getAbsolutePath() 加载到 webView 但 webview 客户端不调用任何回调! 音频文件将不会启动,因为我们使用 javascript 函数来执行此操作。

据我所知,不必更改应用程序的特定目录,因为即使在 Android 11 上,应用程序也有权读取和写入自己创建的文件。

当我将它改回 SDK 29 时,一切正常。

我不知道该怎么办。有人可以帮我吗?

下班后我自己修好了!

我的假设是正确的。该应用程序可以访问这些文件,因为它们是该应用程序自己创建的。所以这里读取文件没有问题...

这是我的解决方法,我希望它能对以后的人有所帮助: WebView 的设置中缺少某些内容。

webView.getSettings().setAllowFileAccess(true);

这让我现在可以从 Android 11.

的应用特定目录加载 html 文件(具有绝对路径)

这是我的代码:

    webView.getSettings().setAllowFileAccess(true);

    File extDir = cxt.getExternalFilesDir(null);
    //htmlPath is something like this: "ExampleApp/EnginePayload/de/html-file.html"
    File twine = new File(extDir.toString() + "/" + htmlPath);

    webView.loadUrl(twine.getAbsolutePath());