29

I have to load an existing html file into a WebView that is located at this path in the file system:

/data/data/com.example.example/files/file.html

But, when the WebView loads it, I don't see anything. Who can help me?

WebView code (assuming path is the path I've written above):

 WebView webView = (WebView)findViewById(R.id.webView1);

  File htmlFile = new File(path);
    if(htmlFile.exists())
    {
        webView.loadUrl(htmlFile.getAbsolutePath());

    }
Amiraslan
  • 794
  • 1
  • 8
  • 19
donadev
  • 439
  • 2
  • 7
  • 17

4 Answers4

53

Try this, adding in a file:/// and doing it a little differently:

WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///data/data/com.example.example/files/file.html");  

Instead of this, however, you could just put the file into your assets folder in the source code, and then do this:

WebView webView = (WebView)findViewById(R.id.webView1);
webview.loadUrl("file:///android_asset/file.html");
hichris123
  • 10,145
  • 15
  • 56
  • 70
30

The html file should be placed in the assets folder, which will belong in the root directory of your project.

So move your file to in case of eclipse

assets/index.html

In an Android Studio project use this folder:

/app/src/main/assets/index.html

Now use

WebView wv= (WebView)findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/index.html");
Nauman Ash
  • 957
  • 1
  • 8
  • 19
7

You need to implement a ContentProvider to map local files to uris as explained in this link how to display a local file into Android Webview

or you just load any html page from Assets folder like below:

 WebView wv= (WebView)findViewById(R.id.webView1);
 wv.loadUrl("file:///android_asset/yourfile.html");
 wv.getSettings().setJavaScriptEnabled(true);
M D
  • 47,665
  • 9
  • 93
  • 114
0

Try this:

web = (Webview) findViewById(R.id.webview);
web.setWebClient(new WebViewClient());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setAllowFileAccess(true);
web.getSettings().setAllowFileAccessFromFileURLs(true);
Dave2e
  • 22,192
  • 18
  • 42
  • 50
cake
  • 409
  • 2
  • 6