package com.example.asynctask;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.widget.ImageView;
public class LoadImgUrl {
private ImageView itemImg;
private String imgUrl;
private LruCache<String, Bitmap> mImgCache;
private Map<String, Thread> threadMap = new HashMap<String, Thread>();
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
if(itemImg.getTag()!= null && itemImg.getTag().equals(imgUrl)){
ImageView threadImg = ((ItemBean)msg.obj).msgImg;
Bitmap bitMap = ((ItemBean)msg.obj).bitMap;
threadImg.setImageBitmap(bitMap);
}
};
};
public LoadImgUrl() {
// TODO Auto-generated constructor stub
int maxMemory = (int) Runtime.getRuntime().maxMemory() / 4;
mImgCache = new LruCache<String, Bitmap>(maxMemory);
}
public void setImgUrl(ImageView img,final String url,int imgHolder) {
this.itemImg = img;
this.imgUrl = url;
if (img.getTag() == null || !img.getTag().equals(url)) {
img.setBackgroundResource(imgHolder);
img.setTag(url);
}
Bitmap imgBitmap = getBitMapByLruc(url);
if (imgBitmap == null) {
// setBitmapByAsyncTask(url, img);
Thread imgThread = threadMap.get(url);
if (imgThread == null) {
setBitmapByNewThread(url,img);
}
}else {
img.setImageBitmap(imgBitmap);
}
}
private void setBitmapByNewThread(final String url,final ImageView threadImg) {
Thread newThread = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
BufferedInputStream is = null;
try {
is = new BufferedInputStream(new URL(url).openStream());
Thread.sleep(3000);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bitMap = BitmapFactory.decodeStream(is);
putBitmapToLruc(bitMap, url);
Message msg = Message.obtain();
ItemBean itemBean = new ItemBean(threadImg, bitMap);
msg.obj = itemBean;
mHandler.sendMessage(msg);
}
};
newThread.start();
threadMap.put(url, newThread);
}
private void setBitmapByAsyncTask(String url,ImageView imgView) {
new MyImgAsyncTask(url, imgView).execute();
}
private Bitmap getBitMapByLruc(String url) {
return mImgCache.get(url);
}
private void putBitmapToLruc(Bitmap bitMap,String url) {
mImgCache.put(url, bitMap);
}
class MyImgAsyncTask extends AsyncTask<String, Progress, Bitmap>{
ImageView asItemImg;
String imgUrl;
public MyImgAsyncTask(String url,ImageView imgView) {
// TODO Auto-generated constructor stub
asItemImg = imgView;
imgUrl = url;
}
@Override
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
Bitmap bitMap = null;
try {
BufferedInputStream bfIs = new BufferedInputStream(new URL(imgUrl).openStream());
bitMap = BitmapFactory.decodeStream(bfIs);
putBitmapToLruc(bitMap, imgUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitMap;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
asItemImg.setImageBitmap(result);
}
}
class ItemBean{
ImageView msgImg;
Bitmap bitMap;
public ItemBean(ImageView img,Bitmap bitmap) {
// TODO Auto-generated constructor stub
msgImg = img;
this.bitMap = bitmap;
}
}
}