Muhammad AbdulSalam
Muhammad AbdulSalam

Reputation: 533

WebView not loading local HTML in Android?

I am trying to load a local html file in a WebView but it always give error new::ERR_FILE_NOT_FOUND I have attached the images of code I am using and the file directory. I have tried it with different directories but it didn't work.

Changed the file directory to res folder, res/raw folder. tried to load different html it does load the webvlinks

Path and the code image is here

Upvotes: 1

Views: 5012

Answers (2)

DHAVAL A.
DHAVAL A.

Reputation: 2321

It is good to place all file assets files in the assets folder. So Add your HTML file to the assets folder and load webview with below code.

webview.loadUrl("file:///android_asset/privacy_policy.html");

Or if you don't want to move the file from that "sampledata" folder, then use below code.

webview.loadUrl("file:///sampledata/privacy_policy.html");

Upvotes: 5

Iosif Costin
Iosif Costin

Reputation: 31

I had the same problem, I found this answer here: https://inneka.com/programming/android/loading-html-file-to-webview-on-android-from-assets-folder-using-android-studio-2/

try {
    AssetManager assetManager = this.getAssets();
    InputStream stream = assetManager.open("editor.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) { 

Upvotes: 3

Related Questions