首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

获取网络图片工具种

2012-07-27 
获取网络图片工具类package com.soarsky.utilimport java.io.ByteArrayOutputStreamimport java.io.File

获取网络图片工具类

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; 
   } 
  
}

热点排行