保存网页以供离线使用并调用相同的android [英] Save webpage for offline use and invoke the same android
本文介绍了保存网页以供离线使用并调用相同的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个android应用程序要求,我需要打开保存的网页,该怎么做?首先,我们如何在Android上保存具有相关性的网页,然后在您的应用程序中打开它?任何输入都会有很大帮助!
I am having an android application requirement where i need to open saved web pages, how to do the same?? FIrst of all, how can we save a webpage with its dependancies on android and later open it in your applications? Any inputs will be of great help!
推荐答案
首先,让我们从 webview
private void initUI{
webView = (WebView) findViewById(R.id.webView);
AndroidWebClient client = new AndroidWebClient();
webView.setWebViewClient(client);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
private class AndroidWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url,
android.graphics.Bitmap favicon) {
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.saveWebArchive(Environment.getExternalStorageDirectory()
+ File.separator +"myArchive"+".mht");
// our webarchive wull be available now at the above provided location with name "myArchive"+".mht"
}
public void onLoadResource(WebView view, String url) {
}
}
所有API中保存webarchive的方式都相同,但加载方式却有所不同
The way to save the webarchive is same in all APIs but to load it, varies
适用于少于Kitkat的API
private void loadArchive(){
String rawData = null;
try {
rawData = getStringFromFile(Environment.getExternalStorageDirectory()
+ File.separator+"myArchive"+".mht");
} catch (Exception e) {
e.printStackTrace();
}
webView.loadDataWithBaseURL(null, rawData, "application/x-webarchive-xml", "UTF-8", null);
}
public String getStringFromFile (String filePath) throws Exception {
File fl = new File(filePath);
FileInputStream fin = new FileInputStream(fl);
String ret = convertStreamToString(fin);
//Make sure you close all streams.
fin.close();
return ret;
}
public String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
Kitkat及更高版本
private void loadArchive(){
webView.loadUrl("file:///"+Environment.getExternalStorageDirectory()
+ File.separator+"myArchive"+".mht");
}
这篇关于保存网页以供离线使用并调用相同的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文