我有一个 android 应用程序要求,我需要打开保存的网页,怎么做?首先,我们如何保存一个依赖于 android 的网页,然后在您的应用程序中打开它?任何输入都会有很大帮助!
问问题
4831 次
2 回答
4
首先,让我们从webview中保存webarchive
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) {
}
}
保存 webarchive 的方式在所有 API 中都是相同的,但加载它的方式各不相同
对于小于 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");
}
于 2016-11-30T07:32:06.550 回答
2
这就是我将如何实现的:
- 将原始网页保存到文件中
- 解析保存的文件并获取所有图像 URL。将图像保存到同一目录中。
- 转换图片的 URL(将所有链接绑定到本地副本)
这是演示这个想法的简单代码:
String download(String url) throws Exception {
String filename = "local.html";
save(url, filename);
List<String> imageLinks = getImageURLs(filename);
for (String imageLink : imageLinks) {
String imageFileName = getImageName(imageLink);
save(imageLink, imageFileName);
}
convertImageURLs(filename);
return filename;
}
void save(String url, String saveTo) throws Exception {
HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
conn.connect();
InputStream is = conn.getInputStream();
save(is, saveTo);
}
void save(InputStream is, String saveTo) {
// save actual content
}
List<String> getImageURLs(String localHtmlFile) {
// parse localHtmlFile and get all URLs for the images
return Collections.EMPTY_LIST;
}
String getImageName(String imageLink) {
// get image name, from url
return null;
}
void convertImageURLs(String localHtmlFile) {
// convert all URLs of the images, something like:
// <img src="original_url"> -> <img src="local_url">
}
于 2013-07-05T09:34:25.860 回答