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

android异步加载图片的更多相关文章

  1. 演化理解 Android 异步加载图片(转)

    演化理解 Android 异步加载图片(转)http://www.cnblogs.com/CJzhang/archive/2011/10/20/2218474.html

  2. 实例演示Android异步加载图片

    本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...

  3. 实例演示Android异步加载图片(转)

    本文给大家演示异步加载图片的分析过程.让大家了解异步加载图片的好处,以及如何更新UI.首先给出main.xml布局文件:简单来说就是 LinearLayout 布局,其下放了2个TextView和5个 ...

  4. [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3574131.html  这个可以实现ImageView异步加载 ...

  5. 演化理解 Android 异步加载图片

    原文:http://www.cnblogs.com/ghj1976/archive/2011/05/06/2038738.html#3018499 在学习"Android异步加载图像小结&q ...

  6. android异步加载图片并缓存到本地实现方法

    图片过多造成内存溢出,这个是最不容易解决的,要想一些好的缓存策略,比如大图片使用LRU缓存策略或懒加载缓存策略.今天首先介绍一下本地缓存图片     在android项目中访问网络图片是非常普遍性的事 ...

  7. Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...

  8. wemall app商城源码中基于JAVA的Android异步加载图片管理器代码

    wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改.本文分享其中 ...

  9. Android之使用Android-AQuery异步加载图片(一)

    第一节:转载地址(http://www.cnblogs.com/lee0oo0/archive/2012/10/25/2738299.html) // 必须实现AQuery这个类 AQuery aq ...

随机推荐

  1. jquery动态加载JS【方法getScript】的改进

    http://www.cnblogs.com/cuitsl/archive/2012/11/15/2771549.html

  2. 二分--LIGHTOJ 1088查找区间(水题)

    #include <iostream> #include <cstdio> #include <cmath> using namespace std; const ...

  3. SQL 语法 Join与Union

    问题描述: Join与Union使用 问题解决: Join连接,可以分为: tableA如下: tableB如下: 1.1.Inner Join SELECT * FROM TableA INNER ...

  4. 【BZOJ】【1004】【HNOI2008】Cards

    Burnside/Polya+背包DP 这道题目是等价类计数裸题吧……>_> 题解:http://m.blog.csdn.net/blog/njlcazl_11109/8316340 啊其 ...

  5. sao/jsp

    sao/i18n/message/ Messages-Client.xml   Messages-Server.xml   sao/wsdl Verification.wsdl   IProcessS ...

  6. Razor语法学习

    原文:http://www.cnblogs.com/youring2/archive/2011/07/24/2115254.html 1.Razor的文件类型 Razor支持两种文件类型,分别是.cs ...

  7. Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...

  8. jdom处理的XML Document 和String 之间的相互转化

    package util; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; ...

  9. Android 检查设备是否存在 导航栏 NavigationBar

    尊重原创.尊重作者,转载请标明出处: http://blog.csdn.net/lnb333666/article/details/41821149 目前也没有可靠的方法来检查设备上是否有导航栏.可以 ...

  10. HDU 1392 Surround the Trees (Graham求凸包周长)

    题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...