/**
*
* @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;
}
}
本人对线程掌握不扎实,希望高手提供一个安全的图片下载源代码参考