Android缓存html图片

package com.example.testwebimgcache;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.os.AsyncTask;
import android.webkit.WebView;

public abstract class HtmlParser extends AsyncTask<Void, Void, String> {

	private String mUrl;
	private WebView webView;
	private static final String TAG = "HtmlParser";
	private Context mContext;
	public static String Js2JavaInterfaceName = "JsUseJava";
	public List<String> imgUrls = new ArrayList<String>();
	
	public HtmlParser(WebView wevView, String url, Context context) {
		this.webView = wevView;
		mUrl = url;
		mContext = context;
	}

	@Override
	protected void onProgressUpdate(Void... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
	}
	
	@Override
	protected String doInBackground(Void... params) {

		Document doc = null;
		imgUrls.clear();

		try {
			doc = Jsoup.parse(new URL(mUrl), 15000);
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		if(doc == null)
			return null;

		Elements es = doc.select("script");
		if(es != null){
			es.remove();
		}
		handleImageClickEvent(doc);
		removeHyperlinks(doc);
		String htmlText = handleDocument(doc);

		return htmlText;
	}
	
	public List<String> getImgUrls(){
		return imgUrls;
	}

	private void handleImageClickEvent(Document doc) {

		Elements es = doc.getElementsByTag("img");

		for (Element e : es) {
			String imgUrl = e.attr("src");
			imgUrls.add(/*"http://hot.qianyan001.com"+*/imgUrl);
			String imgName;
			File file = new File(imgUrl);
			imgName = file.getName();
			if(imgName.endsWith(".gif")){
				e.remove();
			}else{
				
				String filePath = "file:///data/data/com.example.testwebimgcache/files/test/" + imgName;
				e.attr("src","file:///android_asset/web_logo.png");
				e.attr("src_link", filePath);
				e.attr("ori_link",imgUrl);
				String str = "window." + Js2JavaInterfaceName + ".setImgSrc('"
						+ filePath + "')";
				e.attr("onclick", str);
				
				System.out.println("HtmlParser filePath ----> " + filePath);
				
			}
		}
		
		//需要添加的javascript标签
//		var thisImg = document.getElementsByTagName("img");
//		var thisImgsrc = thisImg.getAttribute("src");
//		thisImg[0].setAttribute("src",thisImgsrc);

	}
	
	private void removeHyperlinks(Document doc){
		Elements hrefs = doc.getElementsByTag("a");
        for(Element href : hrefs){
        	href.removeAttr("href");
        }
	}

	protected abstract String handleDocument(Document doc);

	@Override
	protected void onPostExecute(String result) {
//		Log.i(TAG, result);
		webView.loadDataWithBaseURL(null, result, "text/html", "utf-8", null);
//		webView.loadUrl("");
		super.onPostExecute(result);
	}
	
}

 

已标记关键词 清除标记