30

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());

    }
2
  • Does htmlFile.exists() return true? If so what do you see in logcat? Commented Jan 1, 2014 at 22:27
  • htmlFile.exists() always return true... i tried to put a log into the if and the logcat show that log... Commented Jan 2, 2014 at 10:54

4 Answers 4

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");
Sign up to request clarification or add additional context in comments.

Comments

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");

2 Comments

in my project there is not folder with the name android_asset
then create the assets folder
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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.