获取网络图片工具类

package com.soarsky.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;

public class AsyncImageLoader {
private HashMap<String, SoftReference<Drawable>> imageCache;
public AsyncImageLoader() {
imageCache = new HashMap<String, SoftReference<Drawable>>();
}

/**
* 获取远程图片方法
* @param imageUrl 图片远程路径 如:http://hiphotos.baidu.com/zz%B7%E7%B2%D0/pic/item/f8e68dfc5c15f6476c22ebb0.jpg
* @param imageCallback 回调方法
* @return drawable 图片
*/
public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
if (imageCache.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
Drawable drawable = softReference.get();
//缓存中存在,则使用缓存中的图片
if (drawable != null) {
return drawable;
}
}
//handler调用 获取图片方法
final Handler handler = new Handler() {
public void handleMessage(Message message) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}
};

//建立线程获取图片
new Thread() {
@Override
public void run() {
Drawable drawable = loadImageFromUrl(imageUrl);
//如果获取图片不为空,则存入缓存
if(drawable != null){
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
}
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
}
}.start();

return null;
}
/**
* 获取图片方法,添了了线程同步
* @param url 图片路径
* @return
*/
public synchronized static Drawable loadImageFromUrl(String url) {
Bitmap bitmap = null;
URL m = null;
InputStream i = null;
Drawable d = null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

if(m != null) {
//图片大小大于300k时显示为原图1/4
if(m.getFile().length() > 300 * 1024){

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
//bitmapOptions.inJustDecodeBounds = true;
//设置图片长宽为原图的1/2
bitmapOptions.inSampleSize = 2;
bitmap = BitmapFactory.decodeStream(i, null , bitmapOptions);
}else{
bitmap = BitmapFactory.decodeStream(i);
}
}

if(i != null){
try {
i.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if(bitmap != null) {
d = new BitmapDrawable(bitmap);
}
return d;
}

public interface ImageCallback {
public void imageLoaded(Drawable imageDrawable, String imageUrl);
}

//待用方法
public byte[] readInputStream(InputStream is) {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int length=-1;
try {
while((length=is.read(buffer))!=-1){
baos.write(buffer, 0, length);
}
baos.flush();
} catch (IOException e) {
e.printStackTrace();
}
byte[] data=baos.toByteArray();
try {
is.close();
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值