android异步加载图片

 

import java.io.BufferedOutputStream;  

  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.MalformedURLException;  
  8. import java.net.URL;  
  9. import java.util.WeakHashMap;  
  10.   
  11. import org.apache.http.HttpStatus;  
  12.   
  13. import com.android.lalala.util.lalalaApplication;  
  14.   
  15. import android.graphics.Bitmap;  
  16. import android.graphics.Bitmap.CompressFormat;  
  17. import android.graphics.BitmapFactory;  
  18. import android.os.Handler;  
  19. import android.widget.ImageView;  
  20.   
  21. /** 
  22.  * 图片下载与缓存 思路是,先查看内存,后检查SDcard,没有的话联网进行下载。 
  23.  */  
  24. public class ImageLoader {  
  25.     private ImageView imageView = null;  
  26.     private String urlPath = "";  
  27.     private WeakHashMap<String, Bitmap> picsHashMap = null;  
  28.     private String urlHashCode = "";  
  29.     private String filePath = "";  
  30.     private Handler handler = null;  
  31.     private Bitmap handlerBitmap = null;  
  32.   
  33.     /** 
  34.      * 构造器 
  35.      *  
  36.      * @param imageView 
  37.      *            imageview对象 
  38.      * @param urlPath 
  39.      *            下载的url地址 
  40.      * @param filePath 
  41.      *            缓存文件夹名称 
  42.      */  
  43.   
  44.     public ImageLoader(ImageView imageView, String urlPath, String filePath) {  
  45.         super();  
  46.         this.imageView = imageView;  
  47.         this.urlPath = urlPath;  
  48.         this.filePath = filePath;  
  49.         urlHashCode = String.valueOf(urlPath.hashCode());  
  50.         // 从application中获取picHashMap对象  
  51.         picsHashMap = lalalaApplication.getInstance().getPicHashMap();  
  52.         handler = new Handler();  
  53.         new imageLoaderThread().start();  
  54.     }  
  55.       
  56.     /** 
  57.      * 图片下载线程 
  58.      */  
  59.     private class imageLoaderThread extends Thread {  
  60.         @Override  
  61.         public void run() {  
  62.             super.run();  
  63.             if (readFromRAM()) {  
  64.                 return;  
  65.             }  
  66.             if (readFromSDcard()) {  
  67.                 return;  
  68.             }  
  69.             httpDownload();  
  70.         }  
  71.     }  
  72.   
  73.     /** 
  74.      * 开始下载 
  75.      */  
  76.     private void httpDownload() {  
  77.         try {  
  78.             URL url = new URL(urlPath);  
  79.             HttpURLConnection connection = (HttpURLConnection) url  
  80.                     .openConnection();  
  81.             connection.setConnectTimeout(10 * 1000);  
  82.             if (connection.getResponseCode() == HttpStatus.SC_OK) {  
  83.                 InputStream is = connection.getInputStream();  
  84.                 Bitmap bitmap = BitmapFactory.decodeStream(is);  
  85.                 setBitmap(bitmap);  
  86.                 lalalaApplication.getInstance().getPicHashMap()  
  87.                         .put(urlHashCode, bitmap);  
  88.                 saveToSDcard(bitmap);  
  89.             }  
  90.   
  91.         } catch (MalformedURLException e) {  
  92.             e.printStackTrace();  
  93.         } catch (IOException e) {  
  94.             e.printStackTrace();  
  95.         }  
  96.     }  
  97.   
  98.     /** 
  99.      * 将bitmap保存至SD卡上 
  100.      *  
  101.      * @param bitmap 
  102.      *            bitmap 
  103.      */  
  104.     private void saveToSDcard(Bitmap bitmap) {  
  105.         try {  
  106.             String fileName = filePath + "/" + urlHashCode + ".JPG";  
  107.         File file = new File(filePath);  
  108.         if (!file.exists()) {  
  109.             file.mkdir();  
  110.         }  
  111.             BufferedOutputStream outputStream = new BufferedOutputStream(  
  112.                     new FileOutputStream(new File(fileName)));  
  113.             bitmap.compress(CompressFormat.JPEG, 100, outputStream);  
  114.         } catch (FileNotFoundException e) {  
  115.             e.printStackTrace();  
  116.         }  
  117.   
  118.     }  
  119.   
  120.     /** 
  121.      * 从内存中读取bitmap图片数据 
  122.      *  
  123.      * @return true内存中有数据 false 内存中无数据 
  124.      */  
  125.     private boolean readFromRAM() {  
  126.         if (picsHashMap.containsKey(urlHashCode)) {  
  127.             Bitmap bitmap = picsHashMap.get(urlHashCode);  
  128.             setBitmap(bitmap);  
  129.             return true;  
  130.         }  
  131.         return false;  
  132.     }  
  133.   
  134.     /** 
  135.      * 从SD卡读取图片 
  136.      *  
  137.      * @return trueSDcard中有数据 false SDcard中无数据 
  138.      */  
  139.     private boolean readFromSDcard() {  
  140.         String fileName = filePath + "/" + urlHashCode + ".JPG";  
  141.         File file = new File(fileName);  
  142.         if (!file.exists()) {  
  143.             return false;  
  144.         } else {  
  145.             Bitmap bitmap = BitmapFactory.decodeFile(fileName);  
  146.             picsHashMap.put(urlHashCode, bitmap);  
  147.             setBitmap(bitmap);  
  148.             return true;  
  149.         }  
  150.   
  151.     }  
  152.   
  153.     /** 
  154.      * 设置图片 
  155.      *  
  156.      * @param bitmap 
  157.      *            图片 
  158.      */  
  159.     private void setBitmap(Bitmap bitmap) {  
  160.         this.handlerBitmap = bitmap;  
  161.         handler.post(new Runnable() {  
  162.             @Override  
  163.             public void run() {  
  164.                 imageView.setImageBitmap(handlerBitmap);  
  165.             }  
  166.         });  
  167.     }  
  168.   
  169.   
  170.   
  171. }  
原文地址:https://www.cnblogs.com/new0801/p/6175898.html