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?
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.
First , display the content of the file you download in the LogCat, and see if the download is okey or not
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.
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);
精彩评论