本文只是记录一下零碎的东西
SoftReference 即对象的软引用。如果一个对象具有软引用,内存空间足够,垃 圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。软引用可用来实现内存敏感的高 速缓存。使用软引用能防止内存泄露,增强程序的健壮性。
SoftReference的特点是它的一个实例保存对一个Java对象的软引用, 该软引用的存在不妨碍垃圾收集线程对该Java对象的回收。也就是说,一旦SoftReference保存了对一个Java对象的软引用后,在垃圾线程对 这个Java对象回收前,SoftReference类所提供的get()方法返回Java对象的强引用。另外,一旦垃圾线程回收该Java对象之 后,get()方法将返回null
下面看看一个工具类,根据网络图片地址下载图片,UI上显示,并缓存,第二次使用图片时先查缓存
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
/**
* 根据 url 获取用户头像
*
* @author chenling0418
*
*/
public class UserImgHttp {
private static Map<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();
private static Bitmap bitmap;
private static Handler handler = new Handler();;
/**
* 下载图片在显示
*
* @param handler UI线程 handler
* @param view 需要显示图片的 ImageView
* @param url 图片的网络地址
*/
public static void setUserImg2(final ImageView view,final String url) {
Log.i("slack", url);
bitmap = null;
//从缓存中取软引用的Bitmap对象
SoftReference<Bitmap> bitmapcache_ = imageCache.get(url);
//取出Bitmap对象,如果由于内存不足Bitmap被回收,将取得空
if(bitmapcache_ != null){
bitmap = bitmapcache_.get();
}
if(bitmap != null){
Log.i("slack", "bitmap_ != null");
changeView(view,bitmap);
}else{
new Thread(new Runnable() {
@Override
public void run() {
InputStream inputStream = null;
ByteArrayOutputStream out = null;
try {
URL localURL = new URL(url);
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
if (httpURLConnection.getResponseCode() >= 300) {
Log.i("slack","error:"+httpURLConnection.getResponseCode());
throw new Exception(
"HTTP Request is not success, Response code is "
+ httpURLConnection.getResponseCode());
}
inputStream = httpURLConnection.getInputStream();
out = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int n=0;
while ( (n=inputStream.read(buffer)) !=-1) {
out.write(buffer,0,n);
}
//本地缓存图片
out.close();
//强引用的Bitmap对象
bitmap = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.toByteArray().length);
//软引用的Bitmap对象
SoftReference<Bitmap> bitmapcache = new SoftReference<Bitmap>(bitmap);
//添加该对象到Map中使其缓存
imageCache.put(url,bitmapcache);
changeView(view,bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("slack","error:"+e.toString());
}finally{
try {
inputStream.close();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
}
private static void changeView(final ImageView view,final Bitmap bitmap) {
handler.post(
new Runnable() {
@Override
public void run() {
view.setImageBitmap( bitmap );
}
});
}
}
怎么使用:UserImgHttp.setUserImg2(mUsericon, "url");