Andressa Pinheiro
Andressa Pinheiro

Reputation: 1637

Load html page on webview without using a web server

Is there a way to load an html page on a webview without using a web server?

In my android application I have a web server because the user can save the web pages he wants and he can access them offline later.

I tried the assets folder but I cannot modify it at runtime. I can just read files I´ve put there.

To load online or offline I use the methods of the webview: browser is the webview.

    browser.setWebViewClient(new client());
    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setDomStorageEnabled(true);
    browser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    browser.getSettings().setUseWideViewPort(true);
    browser.loadUrl(url);

The url can be :

http://stackoverflow.com a normal one

or

http://localhost:8080"; //the web server inside the app

or from assets folder

files:///android_asset/file.html

But how I said, the use can save many pages and see it when he wants.

Is there a method to load the page saved in internal memory on the webview?

   URL =this.getApplicationContext().getFilesDir()+"/www/file.html";
        browser.loadUrl(URL);

For example: webview.loadString(); ?

Upvotes: 1

Views: 1440

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

Something like browser.loadUrl(Uri.fromFile(f).toString()) should work, for a File object named f pointing to your desired file. Uri.fromFile() will give you a Uri with the correct scheme; toString() gives you the string representation to hand to the WebView. I use this (or a variation) to read files on external storage. AFAIK, it should work fine for files on internal storage as well.

Upvotes: 3

Related Questions