Manika
Manika

Reputation: 41

WebView local files is not loading page in Android 9.0?

mWebView.loadUrl("file:///android_asset/html/index.html");

On older versions, webview for local files is working fine, but when I decided to test on android 9 It gives a blank page.           

I have tried WebView is not loading page in Android 9.0? these solutions.          

Upvotes: 4

Views: 903

Answers (1)

Rahul Khurana
Rahul Khurana

Reputation: 8835

Try putting below code in the AndroidManifest.xml

<application
    ...
   android:usesCleartextTraffic="true"
    ...>
</application>

What it does

It allows your app to access non secure(Non-Https) based url. See Security Config

From Documentation

Note: The guidance in this section applies only to apps that target Android 8.1 (API level 27) or lower. Starting with Android 9 (API level 28), cleartext support is disabled by default.

EDIT

try {
    AssetManager assetManager = context.getAssets();
    InputStream stream = assetManager.open("html/index.html");
    BufferedReader r = new BufferedReader(new InputStreamReader(stream));
    StringBuilder total = new StringBuilder();
    String line;
    while ((line = r.readLine()) != null) {
        total.append(line).append("\n");
    }
    webView.loadDataWithBaseURL(null, total.toString(), "text/html", "UTF-8", null);
} catch (Exception xxx) {
    Log.e(TAG, "Error loading html/index.html", xxx);
}

Upvotes: 1

Related Questions