piolay
piolay

Reputation: 51

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

My App has the following use case:

We download a geojson file, a html file and some audio files from a server for a specific language a user has chosen and we are saving this in the app specific file directory.

The directory path is the following for each use case:

For the HTML file we have:

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

For the json file we have:

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

Audiodata:

/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

and so on...

Since i have to change the minSDK and compileSDK to 30 i don't get the files to work. I load the html-file.getAbsolutePath() to the webView but the webview client does not call ANY callback! The audio files will not be started, because we use javascript functions to do that.

As i unterstand it, the app specific directory does not have to be changed because even on Android 11, the app has permission to read and write its own created files.

The moment i change it back to SDK 29, everything is working.

I don't know what to do. Can someone help me please?

Upvotes: 2

Views: 952

Answers (1)

piolay
piolay

Reputation: 51

I fixed it myself after hours!

My assumptions were right. The app had access to the files, because the app created them itself. so there was not an issue here to read the files...

Here is my fix and i hope it will help someone in the future: There was something missing in the settings for the WebView.

webView.getSettings().setAllowFileAccess(true);

This allows me now to load a html file (with absolute Path) from the app specific directory for Android 11.

Here is my code:

    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());

Upvotes: 2

Related Questions