package com.example.gridview;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
@SuppressLint("HandlerLeak")
public class AsyncBitmapLoader{
/**
* 内存图片软引用缓
*/
@SuppressWarnings("rawtypes")
private final HashMap<String, SoftReference> imageCache = new HashMap<String, SoftReference>();
@SuppressWarnings("rawtypes")
public Bitmap loadBitmap(final ImageView imageView, final String imageURL, final ImageCallBack imageCallBack){
SoftReference reference = imageCache.get(imageURL);
//在内存缓存中,则返回Bitmap对象
if (null != reference && null != reference.get()) {
return (Bitmap) reference.get();
} else {
if (null != reference) {
imageCache.remove(reference);
}
// 加上一个对本地缓存的查找
String bitmapName = toTransverseLine(imageURL.substring(imageURL.indexOf("//") + ));
File cacheDir = new File("/mnt/sdcard/text/");
File[] cacheFiles = cacheDir.listFiles();
int i = ;
if (null != cacheFiles) {
for (i = ; i < cacheFiles.length;i++ ){
if (bitmapName.equals(cacheFiles[i].getName())) {
break;
}
}
if (i < cacheFiles.length) {
return BitmapFactory.decodeFile("/mnt/sdcard/text/" + bitmapName);
}
}
}
final Handler handler = new Handler(){
public void handleMessage(Message msg){
imageCallBack.imageLoad(imageView, (Bitmap)msg.obj);
}
};
//如果不在内存缓存中,也不在本地(被jvm回收掉),则开启线程下载图片
new Thread(){
public void run(){
InputStream bitmapIs = getStreamFromURL(imageURL);
Bitmap bitmap = BitmapFactory.decodeStream(bitmapIs);
if (bitmap == null ) {
return ;
}
imageCache.put(imageURL, new SoftReference(bitmap));
Message msg = handler.obtainMessage(, bitmap);
handler.sendMessage(msg);
File dir = new File("/mnt/sdcard/text/");
if (!dir.exists()) {
dir.mkdirs();
}
// 下载图片保存到指定路径
String path = "/mnt/sdcard/text/" +
toTransverseLine(imageURL.substring(imageURL.indexOf("//") + ));
String path1 = "/mnt/sdcard/image/";
File bitmapFile = new File(path);
FileOutputStream fos = null;
if (!bitmapFile.exists()) {
try{
bitmapFile.createNewFile();
fos = new FileOutputStream(bitmapFile);
bitmap.compress(Bitmap.CompressFormat.PNG,, fos);
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}.start();
return null;
}
/**
* 转换名称保存
* @param url
* @return
*/
private String toTransverseLine(String url) {
String name1 = url.replaceAll("/", "_");
String name2 = name1.replaceAll("\\.", "_");
String name3 = name2.replaceAll("\\:", "_");
return name3;
}
public interface ImageCallBack{
public void imageLoad(ImageView imageView, Bitmap bitmap);
}
/**
* 通过url请求图片
* @return
*/
public static InputStream getStreamFromURL(String imageURL) {
InputStream in=null;
try {
if (imageURL != null && !imageURL.equals("")) {
URL url=new URL(imageURL);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
in=connection.getInputStream();
}
} catch (Exception e) {
e.printStackTrace();
}
return in;
}
// =======================下边是调用 写在activity中========================
private String mNewsPicUrl; // 新闻图片的url
private AsyncBitmapLoader asyncBitmapLoader; // 异步加载
private ImageView mHeadImage;
/**
* 显示图片
*
*/
private void showImage() {
if (mNewsPicUrl != null && !mNewsPicUrl.equals("")) {
Bitmap bitmap = asyncBitmapLoader.loadBitmap(mHeadImage, mNewsPicUrl, new ImageCallBack() {
public void imageLoad(ImageView imageView, Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
});
if (bitmap != null) {
mHeadImage.setImageBitmap(bitmap);
}else {
// mHeadImage.setVisibility(View.GONE);
}
}else {
mHeadImage.setVisibility(View.GONE);
}
}
}