user616076
user616076

Reputation: 4001

WebView not displaying data from html file

I have a page on my Android app which will display the contents of an html file using the code below -

 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.intro);

      WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
       } catch (IOException e) {
          e.printStackTrace();
       }
 }

The code runs ok but the contents don't show in the webview, what am I doing wrong?

Upvotes: 0

Views: 1600

Answers (4)

Omar El Hussein
Omar El Hussein

Reputation: 1057

My following code loads html-content from a Url and displays it in the webview:

MainActivity.java

String htmlContent = getHtmlContent();
            if (htmlContent != null) {
                webView.getSettings().setBuiltInZoomControls(true);
                webView.loadData(htmlContent, fileType.endsWith("rfc822") ? "rfc822" : "text/html", "UTF-8");
            }

getHtmlContent Method

DownloadTask task = new DownloadTask();

    try {
        String result = String.valueOf(task.execute(currentFileUrl).get());
        if (task.getStatus() == AsyncTask.Status.RUNNING) {
            loadingProgressBar.setVisibility(View.GONE);
        }
        return result;
    } catch (ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }

    return null;

and the DownloadTask.java class

public class DownloadTask extends AsyncTask<String, Void, StringBuilder> {


@Override
protected StringBuilder doInBackground(String... urls) {

    StringBuilder result = new StringBuilder();
    URL url;
    HttpURLConnection connection;

    try {
        url = new URL(urls[0]);
        connection = (HttpURLConnection) url.openConnection();
        InputStream in = connection.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        int data = reader.read();

        while (data != -1) {
            char current = (char) data;
            result.append(current);
            data = reader.read();
        }

        return result;

    } catch (IOException e) {
        e.printStackTrace();
    }



    return null;
}

}

One problem that I had is the WebView wasn't desplaying the content, I solved it by setting WebView webview = findViewById(R.id.webView); and removing webview = new WebView(this);

Upvotes: 0

Jorgesys
Jorgesys

Reputation: 126455

use

wv.loadUrl("file:///android_asset/Preface.html");

be sure your file Preface.html is inside your android assets/ folder

or if your html file contains javascript code enable javascript support with

WebView wv = (WebView) findViewById(R.id.WebView01);
      try {
          InputStream fin;
          fin = getAssets().open("Preface.html");
          byte[] buffer = new byte[fin.available()];
          fin.read(buffer);
          fin.close();
          wv.loadData(new String(buffer), "text/html", "UTF-8");
    WebSettings webSettings = wv.getSettings();
    wv.setJavaScriptEnabled(true);
       } catch (IOException e) {
          e.printStackTrace();
       }

if this don't solve your problem paste your html code.

Upvotes: 1

Shishir Shetty
Shishir Shetty

Reputation: 2049

Sometimes what happens is,your screen size becomes too small for the content to display on the screen. see to it that you are using a larger display resolution for your screen.

Upvotes: 0

Houcine
Houcine

Reputation: 24181

First , display the content of the file you download in the LogCat, and see if the download is okey or not

Upvotes: 0

Related Questions