多线程下载图片

/**
 * 
 * @author bingshi
 * 
 * @param <T>
 */
public class ImageLoaderManager<T extends Object> {

	private ExecutorService pool;
	private Map<T, List<ImageLoaderListener>> imageListeners = new HashMap<T, List<ImageLoaderListener>>();
	private HashMap<T, SoftReference<Drawable>> imageCache = new HashMap<T, SoftReference<Drawable>>();
	private String root;
	private ImageLoaderRunable<T> runable;

	public ImageLoaderManager(ImageLoaderRunable<T> runable, int nThreads) {
		this.pool = Executors.newFixedThreadPool(nThreads);
		this.root = Environment.getExternalStorageDirectory() + "/";
		this.runable = runable;
	}

	public Drawable downLoad(T url, ImageLoaderListener listener) throws Exception {

		Drawable drawable = null;
		if (url == null) {
			return null;
		}

		boolean isHas = false;
		isHas = imageCache.containsKey(url);

		if (isHas) {
			SoftReference<Drawable> softReference = imageCache.get(url);
			drawable = softReference.get();
			if (drawable != null) {
				return drawable;
			}
		}

		String imgPath = root + url;
		System.out.println(imgPath);

		File imgFile = new File(imgPath);
		if (imgFile.exists()) {
			drawable = Drawable.createFromPath(imgPath);
			if (drawable != null) {
				return drawable;
			}
		}

		isHas = false;
		isHas = imageListeners.containsKey(url);

		if (isHas) {
			if (!imageListeners.containsKey(url)) {
				throw new Exception("ImageLoaderManager error");
			}
			imageListeners.get(url).add(listener);
			return null;
		} else {
			if (!imageListeners.containsKey(url)) {
				imageListeners.put(url, new ArrayList<ImageLoaderListener>());
			}
			imageListeners.get(url).add(listener);
		}

		Runnable thread = new ImageLoaderTask(runable, url);
		pool.execute(thread);

		return null;
	}

	private class ImageLoaderTask implements Runnable {
		private T url;
		private ImageLoaderRunable<T> runable;

		public ImageLoaderTask(ImageLoaderRunable<T> runnable, T url) {
			this.runable = runnable;
			this.url = url;
		}

		@Override
		public void run() {

			BitmapDrawable bitmapdrawable = null;
			Bitmap bitmap = runable.download(url);
			
			if (!isSDCardMounted()) {
				imageCache.put(url, new SoftReference<Drawable>(new BitmapDrawable(bitmap)));
			} else {
				String imgPath = root + url;
				File imgFile = new File(imgPath); 
				try {					
					imgFile.createNewFile();
					FileOutputStream fos = new FileOutputStream(imgFile);
					bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
					fos.flush();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

				bitmapdrawable = (BitmapDrawable) Drawable.createFromPath(imgPath);
			}
			
			for (ImageLoaderListener listener : imageListeners.get(url)) {
				listener.downloadSuccess(bitmapdrawable);
			}
			
			imageListeners.remove(url);
			
		}
	}
	
	/**
	 * 判断SD卡是否存在并可读写
	 * 
	 * @return
	 */
	public static boolean isSDCardMounted() {
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

}


public interface ImageLoaderListener {
	public void downloadSuccess(Drawable drawable);
}

public interface ImageLoaderRunable<T extends Object> {
	public Bitmap download(T url);
}

--------------------------------------
private IMManager(String domain, int port) throws XMPPException {
		messageListenter = new IMMessageListenter(this);
		contactManager = new ContactManager();
		connection = IMUtil.getConnection(domain, port);
		imageLoaderManager = new ImageLoaderManager<String>(new ImageLoaderRunable<String>() {

			@Override
			public Bitmap download(String url) {
				Bitmap bitmap = IMManager.getInstance().getUserHead(url);

				if (bitmap == null) {
					return null;
				}
				return bitmap;
			}
			
		}, 7);
	}
	
	public Drawable downLoad(String user, ImageLoaderListener listener){
		try {
			return imageLoaderManager.downLoad(user, listener);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


本人对线程掌握不扎实,希望高手提供一个安全的图片下载源代码参考

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值