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

    }
2
  • Does htmlFile.exists() return true? If so what do you see in logcat?
    – Greg Ennis
    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...
    – donadev
    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");
0
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
  • in my project there is not folder with the name android_asset May 7, 2016 at 10:48
  • then create the assets folder
    – Nauman Ash
    May 27, 2016 at 12:40
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);
0
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.