Med Zamrik
Med Zamrik

Reputation: 323

WebView does not work with local HTML files

I am trying to use WebView to show a local HTML file containing a table. However, when I use the WebView to check websites online it works perfectly, the problem is when i try to use the local HTML file.

This is the webView part of the code:

WebView myWebView = (WebView) v.findViewById(R.id.webview);
myWebView.loadUrl("file:///asset/table.html");

I have used an online host to test the HTML file with this and it worked perfectly:

myWebView.loadUrl("http://ahmad92billectric.host56.com//home.html");

the HTIML file is in app\src\main\asset, I tried calling it using the following ways:

myWebView.loadUrl("file:///andriod_asset/table.html");
myWebView.loadUrl("file:///app/src/main/assets/table.html");
myWebView.loadUrl("file:///android_assets/table.html"); //this one is assets with an s at the end

I looked up many questions here non of the answers worked for me:
Loading existing .html file with android WebView
What does file:///android_assets/www/index.html mean?
Android webview loadurl("file:///android_asset/index.html#home") failed
Android Studio Assets not loading Local HTML File
I have checked many other questions with no fruitful results. I am using android studio 0.8.14

Upvotes: 1

Views: 1793

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007494

Use file:///android_asset/table.html, which is none of your listed attempts.

You can see that in use in this sample project, though the file here is geoweb1.html:

  @SuppressLint("SetJavaScriptEnabled")
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.main);
    browser=(WebView)findViewById(R.id.webkit);

    myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

    browser.getSettings().setJavaScriptEnabled(true);
    browser.addJavascriptInterface(new Locater(), "locater");
    browser.loadUrl("file:///android_asset/geoweb1.html");
  }

Upvotes: 1

Related Questions