Android, Why my html file doesn't show?
I have stored some of my information in an HTML file and put it in asset folder. However, when I want to show it, web page starts but it says that "Web page not available", while I have stored it in assets folder.
This is开发者_如何转开发 my code:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.am_culture);
final FrameLayout frame01 = (FrameLayout) findViewById(R.id.ch_frame01);
final WebView wv = new WebView(this);
frame01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wv.loadUrl("file:///android_asset/people.html");
setContentView(wv);
}
});
}
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.wb1);
mWebView.getSettings().setJavaScriptEnabled(true);
InputStream is;
try {
is = getAssets().open("people.html");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String strContent = new String(buffer);
mWebView.loadData(strContent, "text/html", "UTF-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This should be your code for retrieving assets' file
public String inputStreamToString()
{
StringBuffer sBuffer = new StringBuffer();
try
{
InputStream is = getResources().getAssets().open("people.html");
byte[] b = new byte[1024];
for (int n; (n = is.read(b)) != -1;) {
sBuffer.append(new String(b, 0, n));
}
}
catch (Exception e) {
// TODO: handle exception
//Log.e("inputstream",e.toString());
}
return sBuffer.toString();
}
public void webview()
{
WebView wb = (WebView) findViewById(R.id.webView1);
wb.getSettings().setJavaScriptEnabled(true);
String summary = inputStreamToString();
wb.loadData(summary, "text/html", "utf-8");
}
精彩评论