80,349
社区成员
发帖
与我相关
我的任务
分享
package com.xiakeng.micapp;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
public class AsyncBitmapLoader {
/**
* 内存图片软引用缓冲
*/
private HashMap<String, SoftReference<Bitmap>> imageCache = null;
public AsyncBitmapLoader() {
imageCache = new HashMap<String, SoftReference<Bitmap>>();
}
public Bitmap loadBitmap(final ImageView imageView, final String imageURL,
final ImageCallBack imageCallBack) {
// 在内存缓存中,则返回Bitmap对象
if (imageCache.containsKey(imageURL)) {
SoftReference<Bitmap> reference = imageCache.get(imageURL);
Bitmap bitmap = reference.get();
if (bitmap != null) {
Log.v("cache", "notnull");
return bitmap;
}
} else {
/**
* 加上一个对本地缓存的查找
*/
String bitmapName = imageURL
.substring(imageURL.lastIndexOf("/") + 1);
File cacheDir = new File(Environment.getExternalStorageDirectory()
.toString() + "/micapp/");
File[] cacheFiles = cacheDir.listFiles();
int i = 0;
if (null != cacheFiles) {
for (; i < cacheFiles.length; i++) {
if (bitmapName.equals(cacheFiles[i].getName())) {
break;
}
}
if (i < cacheFiles.length) {
return BitmapFactory.decodeFile(Environment
.getExternalStorageDirectory().toString()
+ "/micapp/" + bitmapName);
}
}
}
final Handler handler = new Handler() {
/*
* (non-Javadoc)
*
* @see android.os.Handler#handleMessage(android.os.Message)
*/
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
imageCallBack.imageLoad(imageView, (Bitmap) msg.obj);
}
};
// 如果不在内存缓存中,也不在本地(被jvm回收掉),则开启线程下载图片
new Thread() {
/*
* (non-Javadoc)
*
* @see java.lang.Thread#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
InputStream bitmapIs = HttpUtils.getStreamFromURL(imageURL);
Bitmap bitmap = BitmapFactory.decodeStream(bitmapIs);
imageCache.put(imageURL, new SoftReference<Bitmap>(bitmap));
Message msg = handler.obtainMessage(0, bitmap);
handler.sendMessage(msg);
File dir = new File(Environment.getExternalStorageDirectory()
.toString() + "/micapp");
File testdir = new File(Environment
.getExternalStorageDirectory().toString());
if (!dir.exists()) {
boolean b = dir.mkdirs();
Log.v("sdfile", Environment.getExternalStorageState()
.toString());
Log.v("sdfile", b + "");
Log.v("sdfile", testdir.canRead() + "canread?");
Log.v("sdfile", testdir.canWrite() + "canwrite?");
Log.v("sdfile", Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED) + "写入权限?");
}
File bitmapFile = new File(Environment
.getExternalStorageDirectory().toString()
+ "/micapp/"
+ imageURL.substring(imageURL.lastIndexOf("/") + 1));
if (!bitmapFile.exists()) {
try {
bitmapFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream fos;
try {
fos = new FileOutputStream(bitmapFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
return null;
}
public interface ImageCallBack {
public void imageLoad(ImageView imageView, Bitmap bitmap);
}
}