Skip to content

Use the Webview to load local asset files and html formated strings

rutura edited this page Apr 16, 2017 · 1 revision
  • MinSdk : 3
  • Use it to load local resources stored in the assed directory
  • The assets directory must be named exactly "assets"
  • The assets directory must be at the same level as "java" and "res"
  • Load a local asset as shown below:
         upperView.loadUrl("file:///android_asset/testfile.png");
  • "file:///android_asset/" must be there exactly as is.The part following it is just your directory structure
  • Load a html formated string resouce as shown below:
         lowerView.loadData(htmlString, "text/html", "utf-8");
  • You can even directly pass in html formated strings to be shown:
        WebView lowerView = (WebView)findViewById(R.id.lowerview);
        String htmlString =
                "<h1>Header</h1><p>This is HTML text<br />"
                        + "<i>Formatted in italics</i></p>";
        lowerView.loadData(htmlString, "text/html", "utf-8");
Clone this wiki locally