listview 图片异步加载 的实现并缓存到内存 和本地sdcard 中

package com.dodoyota.weicom.net;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import com.weibo.sdk.android.util.FormatTools;
import com.weibo.sdk.android.util.MD5Utils;

/**
 * 异步加载图片
 * @author 高元东
 * @mailto [email protected]
 * 2013-6-20 下午9:58:57
 */
public class AsyncImageLoader {
	
	public Map<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>();
	private static  ExecutorService executorService = Executors.newFixedThreadPool(5);
	private final Handler handler = new Handler();
	
	
	/***
	 * 异步加载图片
	 * @mailto [email protected]
	 * 2013-6-20 下午10:01:34
	 * @param imageUrl
	 * @param callback
	 * @return
	 */
	public Drawable loadDrawable(final String imageUrl,
			final ImageCallback callback) {
		// 如果缓存过就从缓存中取出数据
		if (imageCache.containsKey(MD5Utils.encode(imageUrl))) {
			SoftReference<Drawable> softReference = imageCache.get(MD5Utils.encode(imageUrl));
			if (softReference.get() != null) {
				Log.i("imageloader", "从缓存中获取图片");
				return softReference.get();
			}
		}
		
		//sdcard 可用使用
		  if(this.checkSdcard()) {
			  Drawable drawable = null;
			  File dir = new File(Environment.getExternalStorageDirectory()+"/weicom");
			   if(!dir.exists()) {
				   dir.mkdir();
			   }
			   
			   File header = new File(dir, MD5Utils.encode(imageUrl));
			   if(header.exists()) {
				   FileInputStream fileInputStream = null;
				try {
					fileInputStream = new FileInputStream(header);
					//从sdcard 中取数据
					
					    drawable = Drawable.createFromStream(fileInputStream, "ayscimage");
					    Log.i("imageloader", "从sdcard获取图片");
					   if(!imageCache.containsKey(MD5Utils.encode(imageUrl))) {
						   //在sdcard 中取出的图片放在缓存中
						   imageCache.put(MD5Utils.encode(imageUrl), new SoftReference<Drawable>(drawable));
					   }
					   return drawable;
				} catch (Exception e) {
					e.printStackTrace();
				}
				   
			   }
			  
		  }
		// 缓存中没有图像,则从网络上取出数据,并将取出的数据缓存到内存中
		executorService.submit(new Runnable() {
			public void run() {
				try {
					final Drawable drawable = loadImageFromUrl(imageUrl); 
						
					imageCache.put(imageUrl, new SoftReference<Drawable>(
							drawable));
					 
					Log.i("imageloader", "将图片保存在缓存中");
					/***
					 * 保存在sdcard 中
					 */
					if(checkSdcard()) {
						 File dir = new File(Environment.getExternalStorageDirectory()+"/weicom");
						   if(!dir.exists()) {
							   
							   dir.mkdir();
						   }
						   
						   File header = new File(dir, MD5Utils.encode(imageUrl));
						   
						   
							   FileOutputStream fileOutputStream = new FileOutputStream(header);
							   FormatTools formatTools = FormatTools.getInstance();
							   byte[] by = formatTools.Drawable2Bytes(drawable);
							   fileOutputStream.write(by);
							   fileOutputStream.flush();
							   Log.i("imageloader", "将图片保存在sdcard ");
						  
					}

					handler.post(new Runnable() {
						public void run() {
							callback.imageLoaded(drawable);
						}
					});
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		});
		return null;
	}
	
	
	
	/***
	 * 按照 url  从网络上获取 图片
	 * @mailto [email protected]
	 * 2013-6-20 下午10:00:40
	 * @param imageUrl
	 * @return
	 */
	protected Drawable loadImageFromUrl(String imageUrl) {
		try {
			Log.i("imageloader", "从网络中获取图片");
			return Drawable.createFromStream(new URL(imageUrl).openStream(),
					"iamgeSync");

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/***
	 * 检查sdcard 是否可用
	 * @mailto [email protected]
	 * 2013-6-20 下午10:08:18
	 * @return
	 */
	private boolean checkSdcard() {
		
		String status = Environment.getExternalStorageState();
		if(status.equals(Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
		
	}
	/***
	 * 对外开放的接口
	 * @author 高元东
	 * @mailto [email protected]
	 * 2013-6-20 下午10:00:03
	 */
	public interface ImageCallback {
			public void imageLoaded(Drawable imageDrawable);
		}
}

猜你喜欢

转载自qq466862016.iteye.com/blog/1890901