user1063364
user1063364

Reputation: 917

How to load html file in webview on Android 11+ using Storage Access Framework?

I have application which displays content of html files in WebView from local storage. I use standard paths, example :

myWebView.loadUrl("file:///storage/emulated/0/myfolder/myfile.html");

On android 11+ I must use SAF. I can get uri to the file. But how can I pass it to WebView? Or how can I load content of html file in WebView using SAF?

EDIT: I see what is wrong. First file (index.html) is loaded correctly. This is it's uri:

content://com.android.externalstorage.documents/tree/9C33-6BBD%3AGames/document/9C33-6BBD%3AGames%2FWalkthroughs%2Fdoom%2Findex.html

However, it contains local links. WebView callbacks

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
       view.loadUrl(url);
    }

And there is problem. Incoming "String url" is in incorrect format, it is not in correct uri. This is what comes:

content://com.android.externalstorage.documents/tree/9C33-6BBD%3AGames/document/www.abcgames.sk/wsdindex.html@p=navody_zobraz&id=4004.html

The real path is :

/storage/9C33-6BBD/Games/Walkthroughs/doom/www.abcgames.sk/wsdindex.html@p=navody_zobraz&id=4004.html

This is how it was dumped to disk with some web downloader application.

Upvotes: 0

Views: 1019

Answers (1)

blackapps
blackapps

Reputation: 9282

  webView.getSettings().setAllowContentAccess(true);


  webView.loadUrl(uri.toString());

Upvotes: 0

Related Questions