Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18566
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
  • 认证徽章:
文章分类
文章存档

2016年(48)

我的朋友

分类: Java

2016-11-18 15:24:06

wemall doraemon是Android客户端程序,服务端采用wemall微信商城,不对原商城做任何修改,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可随意定制修改。本文分享其中关于异步加载图片管理器,供技术员参考学习。

 

点击(此处)折叠或打开

  1. package com.inuoer.util;

  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.UnsupportedEncodingException;
  8. import java.lang.ref.SoftReference;
  9. import java.net.HttpURLConnection;
  10. import java.net.URL;
  11. import java.security.MessageDigest;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.util.Map;

  14. import android.graphics.Bitmap;
  15. import android.graphics.BitmapFactory;

  16. /**
  17.  *
  18.  * @author Administrator
  19.  * @desc 异步加载图片管理器
  20.  *
  21.  */
  22. public class LoaderImpl {
  23.     //内存中的软应用缓存
  24.     private Map<String, SoftReference<Bitmap>> imageCache;
  25.     
  26.     //是否缓存图片至本地文件
  27.     private boolean cache2FileFlag = false;
  28.     
  29.     //缓存目录,默认是/data/data/package/cache/目录
  30.     private String cachedDir;
  31.     
  32.     public LoaderImpl(Map<String, SoftReference<Bitmap>> imageCache){
  33.         this.imageCache = imageCache;
  34.     }
  35.     
  36.     /**
  37.      * 是否缓存图片至外部文件
  38.      * @param flag
  39.      */
  40.     public void setCache2File(boolean flag){
  41.         cache2FileFlag = flag;
  42.     }
  43.     
  44.     /**
  45.      * 设置缓存图片到外部文件的路径
  46.      * @param cacheDir
  47.      */
  48.     public void setCachedDir(String cacheDir){
  49.         this.cachedDir = cacheDir;
  50.     }
  51.     
  52.     /**
  53.      * 从网络端下载图片
  54.      * @param url 网络图片的URL地址
  55.      * @param cache2Memory 是否缓存(缓存在内存中)
  56.      * @return bitmap 图片bitmap结构
  57.      *
  58.      */
  59.     public Bitmap getBitmapFromUrl(String url, boolean cache2Memory){
  60.         Bitmap bitmap = null;
  61.         try{
  62.             URL u = new URL(url);
  63.             HttpURLConnection conn = (HttpURLConnection)u.openConnection();
  64.             InputStream is = conn.getInputStream();
  65.             bitmap = BitmapFactory.decodeStream(is);
  66.             
  67.             if(cache2Memory){
  68.                 //1.缓存bitmap至内存软引用中
  69.                 imageCache.put(url, new SoftReference<Bitmap>(bitmap));
  70.                 if(cache2FileFlag){
  71.                     //2.缓存bitmap至/data/data/packageName/cache/文件夹中
  72.                     String fileName = getMD5Str(url);
  73.                     String filePath = this.cachedDir + "/" +fileName;
  74.                     FileOutputStream fos = new FileOutputStream(filePath);
  75.                     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
  76.                 }
  77.             }
  78.             
  79.             is.close();
  80.             conn.disconnect();
  81.             return bitmap;
  82.         }catch(IOException e){
  83.             e.printStackTrace();
  84.             return null;
  85.         }
  86.     }
  87.     
  88.     /**
  89.      * 从内存缓存中获取bitmap
  90.      * @param url
  91.      * @return bitmap or null.
  92.      */
  93.     public Bitmap getBitmapFromMemory(String url){
  94.         Bitmap bitmap = null;
  95.         if(imageCache.containsKey(url)){
  96.             synchronized(imageCache){
  97.                 SoftReference<Bitmap> bitmapRef = imageCache.get(url);
  98.                 if(bitmapRef != null){
  99.                     bitmap = bitmapRef.get();
  100.                     return bitmap;
  101.                 }
  102.             }
  103.         }
  104.         //从外部缓存文件读取
  105.         if(cache2FileFlag){
  106.             bitmap = getBitmapFromFile(url);
  107.             if(bitmap != null)
  108.                 imageCache.put(url, new SoftReference<Bitmap>(bitmap));
  109.         }
  110.         
  111.         return bitmap;
  112.     }
  113.     
  114.     /**
  115.      * 从外部文件缓存中获取bitmap
  116.      * @param url
  117.      * @return
  118.      */
  119.     private Bitmap getBitmapFromFile(String url){
  120.         Bitmap bitmap = null;
  121.         String fileName = getMD5Str(url);
  122.         if(fileName == null)
  123.             return null;
  124.         
  125.         String filePath = cachedDir + "/" + fileName;
  126.         
  127.         try {
  128.             FileInputStream fis = new FileInputStream(filePath);
  129.             bitmap = BitmapFactory.decodeStream(fis);
  130.         } catch (FileNotFoundException e) {
  131.             e.printStackTrace();
  132.             bitmap = null;
  133.         }
  134.         return bitmap;
  135.     }
  136.     
  137.     
  138.     /**
  139.      * MD5 加密
  140.      */
  141.     private static String getMD5Str(String str) {
  142.         MessageDigest messageDigest = null;
  143.         try {
  144.             messageDigest = MessageDigest.getInstance("MD5");
  145.             messageDigest.reset();
  146.             messageDigest.update(str.getBytes("UTF-8"));
  147.         } catch (NoSuchAlgorithmException e) {
  148. // System.out.println("NoSuchAlgorithmException caught!");
  149.             return null;
  150.         } catch (UnsupportedEncodingException e) {
  151.             e.printStackTrace();
  152.             return null;
  153.         }
  154.    
  155.         byte[] byteArray = messageDigest.digest();
  156.         StringBuffer md5StrBuff = new StringBuffer();
  157.         for (int i = 0; i < byteArray.length; i++) {
  158.             if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
  159.                 md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
  160.             else
  161.                 md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
  162.         }
  163.    
  164.         return md5StrBuff.toString();
  165.     }

  166.     /**
  167.      * MD5 加密
  168.     private static String getMD5Str(Object...objects){
  169.         StringBuilder stringBuilder=new StringBuilder();
  170.         for (Object object : objects) {
  171.             stringBuilder.append(object.toString());
  172.         }
  173.         return getMD5Str(stringBuilder.toString());
  174.     }*/
  175. }

原文详情地址:http://git.oschina.net/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

wemall doraemonAndroid app商城详情地址:http://www.koahub.com/home/product/55

wemall官网地址:http://www.wemallshop.com

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

阅读(147) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册