热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android实现图片异步请求加三级缓存

这篇文章主要向大家详细介绍了Android实现图片异步请求加三级缓存的相关资料,需要的朋友可以参考下

使用xUtils等框架是很方便,但今天要用代码实现bitmapUtils 的功能,很简单,

AsyncTask请求一张图片

####AsyncTask

#####AsyncTask是线程池+handler的封装 第一个泛型: 传参的参数类型类型(和doInBackground一致) 第二个泛型:
#####更新进度的参数类型(和onProgressUpdate一致) 第三个泛型: 返回结果的参数类型(和onPostExecute一致,
#####和doInBackground返回类型一致)

看AsyncTask源码:

public abstract class AsyncTask {
 private static final String LOG_TAG = "AsyncTask";

 private static final int CORE_POOL_SIZE = 5;
 private static final int MAXIMUM_POOL_SIZE = 128;
 private static final int KEEP_ALIVE = 1;

 private static final ThreadFactory sThreadFactory = new ThreadFactory() {
  private final AtomicInteger mCount = new AtomicInteger(1);

  public Thread newThread(Runnable r) {
   return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
  }
 };


核心线程5 最大线程128  这是AsyncTask的线程池  然后通过handler发送消息 , 它内部实例化了一个静态的自定义类 InternalHandler,这个类是继承自 Handler 的,在这个自定义类中绑定了一个叫做 AsyncTaskResult 的对象,每次子线程需要通知主线程,就调用 sendToTarget 发送消息给 handler自己。然后在 handler 的 handleMessage 中 AsyncTaskResult 根据消息的类型不同(例如 MESSAGE_POST_PROGRESS 会更新进度条,MESSAGE_POST_CANCEL 取消任务)而做不同的操作,值得一提的是,这些操作都是在UI线程进行的,意味着,从子线程一旦需要和 UI 线程交互,内部自动调用了 handler 对象把消息放在了主线程了。

 private static final InternalHandler sHandler = new InternalHandler();

 mFuture = new FutureTask(mWorker) {
   @Override
   protected void More ...done() {
    Message message;
    Result result = null;
    try {
     result = get();
    } catch (InterruptedException e) {
     android.util.Log.w(LOG_TAG, e);
    } catch (ExecutionException e) {
     throw new RuntimeException("An error occured while executing doInBackground()",
       e.getCause());
    } catch (CancellationException e) {
     message = sHandler.obtainMessage(MESSAGE_POST_CANCEL,
       new AsyncTaskResult(AsyncTask.this, (Result[]) null));
     message.sendToTarget();
     return;
    } catch (Throwable t) {
     throw new RuntimeException("An error occured while executing "
       + "doInBackground()", t);
    }

    message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
      new AsyncTaskResult(AsyncTask.this, result));
    message.sendToTarget();
   }
  };
 private static class InternalHandler extends Handler {
  @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})
  @Override
  public void More ...handleMessage(Message msg) {
   AsyncTaskResult result = (AsyncTaskResult) msg.obj;
   switch (msg.what) {
    case MESSAGE_POST_RESULT:
     // There is only one result
     result.mTask.finish(result.mData[0]);
     break;
    case MESSAGE_POST_PROGRESS:
     result.mTask.onProgressUpdate(result.mData);
     break;
    case MESSAGE_POST_CANCEL:
     result.mTask.onCancelled();
     break;
   }
  }
 }


下面看代码 第一步我们先请求一张图片 并解析 注释写的很详细了.

NetCacheUtils.java

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

/**
 * 网络缓存
 * 
 * @author Ace
 * @date 2016-02-18
 */
public class NetCacheUtils {

 private LocalCacheUtils mLocalUtils;
 private MemoryCacheUtils mMemoryUtils;

 public NetCacheUtils(LocalCacheUtils localUtils,
   MemoryCacheUtils memoryUtils) {
  mLocalUtils = localUtils;
  mMemoryUtils = memoryUtils;
 }

 public void getBitmapFromNet(ImageView imageView, String url) {
  BitmapTask task = new BitmapTask();
  task.execute(imageView, url);
 }

 /**
  * AsyncTask是线程池+handler的封装 第一个泛型: 传参的参数类型类型(和doInBackground一致) 第二个泛型:
  * 更新进度的参数类型(和onProgressUpdate一致) 第三个泛型: 返回结果的参数类型(和onPostExecute一致,
  * 和doInBackground返回类型一致)
  */
 class BitmapTask extends AsyncTask {

  private ImageView mImageView;
  private String url;

  // 主线程运行, 预加载
  @Override
  protected void onPreExecute() {
   super.onPreExecute();
  }

  // 子线程运行, 异步加载逻辑在此方法中处理
  @Override
  protected Bitmap doInBackground(Object... params) {
   mImageView = (ImageView) params[0];
   url = (String) params[1];
   mImageView.setTag(url);// 将imageView和url绑定在一起
   // publishProgress(values)//通知进度

   // 下载图片
   return download(url);
  }

  // 主线程运行, 更新进度
  @Override
  protected void onProgressUpdate(Integer... values) {
   super.onProgressUpdate(values);
  }

  // 主线程运行, 更新主界面
  @Override
  protected void onPostExecute(Bitmap result) {
   if (result != null) {
    // 判断当前图片是否就是imageView要的图片, 防止listview重用导致的图片错乱的情况出现
    String bindUrl = (String) mImageView.getTag();
    if (bindUrl.equals(url)) {
     // 给imageView设置图片
     mImageView.setImageBitmap(result);
 // 将图片保存在本地
     mLocalUtils.setBitmapToLocal(result, url);

     // 将图片保存在内存
     mMemoryUtils.setBitmapToMemory(url, result);
    }
   }
  }

 }

 /**
  * 下载图片
  * 
  * @param url
  */
 public Bitmap download(String url) {
  HttpURLConnection cOnn= null;
  try {
   cOnn= (HttpURLConnection) (new URL(url).openConnection());

   conn.setConnectTimeout(5000);
   conn.setReadTimeout(5000);
   conn.setRequestMethod("GET");

   conn.connect();

   int respOnseCode= conn.getResponseCode();
   if (respOnseCode== 200) {
    InputStream in = conn.getInputStream();
    // 将流转化为bitmap对象
    Bitmap bitmap = BitmapFactory.decodeStream(in);
    return bitmap;
   }

  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (conn != null) {
    conn.disconnect();
   }
  }

  return null;
 }

}


MemoryCacheUtils.java  用到了LruCache 很简单
我简单翻译下文档:

* A cache that holds strong references to a limited number of values. Each time 

* a value is accessed, it is moved to the head of a queue. When a value is 

* added to a full cache, the value at the end of that queue is evicted and may 
* become eligible for garbage collection. 

* Cache保存一个强引用来限制内容数量,每当Item被访问的时候,此Item就会移动到队列的头部。
* 当cache已满的时候加入新的item时,在队列尾部的item会被回收。
* 

If your cached values hold resources that need to be explicitly released, * override {@link #entryRemoved}. * 如果你cache的某个值需要明确释放,重写entryRemoved() *

By default, the cache size is measured in the number of entries. Override * {@link #sizeOf} to size the cache in different units. For example, this cache * is limited to 4MiB of bitmaps: 默认cache大小是测量的item的数量,重写sizeof计算不同item的 * 大小。 {@code * int cacheSize = 4 * 1024 * 1024; // 4MiB * LruCache bitmapCache = new LruCache(cacheSize) { * protected int sizeOf(String key, Bitmap value) { * return value.getByteCount(); * } * }} -------------------------------------------------------------------

This class is thread-safe. Perform multiple cache operations atomically by * synchronizing on the cache:

 {@code 
* synchronized (cache) { 
* if (cache.get(key) == null) { 
* cache.put(key, value); 
* } 
* }}
* 他是线程安全的,自动地执行多个缓存操作并且加锁 -------------------------

This class does not allow null to be used as a key or value. A return * value of null from {@link #get}, {@link #put} or {@link #remove} is * unambiguous: the key was not in the cache. * 不允许key或者value为null * 当get(),put(),remove()返回值为null时,key相应的项不在cache中

最重要的大概就是以上几点: 使用很简单
来看代码:

import android.graphics.Bitmap;
import android.support.v4.util.LruCache;

/**
 * 内存缓存工具类
 * 
 * @author Ace
 * @date 2016-02-19
 */
public class MemoryCacheUtils {


 // Android 2.3 (API Level
 // 9)开始,垃圾回收器会更倾向于回收持有软引用或弱引用的对象,这让软引用和弱引用变得不再可靠,建议用LruCache,它是强引用

 private LruCache mCache;

 public MemoryCacheUtils() {
  int maxMemory = (int) Runtime.getRuntime().maxMemory();// 获取虚拟机分配的最大内存
                // 16M
  // LRU 最近最少使用, 通过控制内存不要超过最大值(由开发者指定), 来解决内存溢出,就像上面翻译的所说 如果cache满了会清理最近最少使用的缓存对象
  mCache = new LruCache(maxMemory / 8) {
   @Override
   protected int sizeOf(String key, Bitmap value) {
    // 计算一个bitmap的大小
    int size = value.getRowBytes() * value.getHeight();// 每一行的字节数乘以高度
    return size;
   }
  };
 }

 public Bitmap getBitmapFromMemory(String url) {

  return mCache.get(url);
 }

 public void setBitmapToMemory(String url, Bitmap bitmap) {

  mCache.put(url, bitmap);
 }

}


最后一级缓存 本地缓存 把网络下载的图片 文件名以MD5的形式保存到内存卡的制定目录

/**
 * 本地缓存工具类
 * 
 * @author Ace
 * @date 2016-02-19
 */
public class LocalCacheUtils {

 // 图片缓存的文件夹
 public static final String DIR_PATH = Environment
   .getExternalStorageDirectory().getAbsolutePath()
   + "/ace_bitmap_cache";

 public Bitmap getBitmapFromLocal(String url) {
  try {
   File file = new File(DIR_PATH, MD5Encoder.encode(url));

   if (file.exists()) {
    Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(
      file));
    return bitmap;
   }

  } catch (Exception e) {
   e.printStackTrace();
  }

  return null;
 }

 public void setBitmapToLocal(Bitmap bitmap, String url) {
  File dirFile = new File(DIR_PATH);

  // 创建文件夹 文件夹不存在或者它不是文件夹 则创建一个文件夹.mkdirs,mkdir的区别在于假如文件夹有好几层路径的话,前者会创建缺失的父目录 后者不会创建这些父目录
  if (!dirFile.exists() || !dirFile.isDirectory()) {
   dirFile.mkdirs();
  }

  try {
   File file = new File(DIR_PATH, MD5Encoder.encode(url));
   // 将图片压缩保存在本地,参1:压缩格式;参2:压缩质量(0-100);参3:输出流
   bitmap.compress(CompressFormat.JPEG, 100,
     new FileOutputStream(file));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

MD5Encoder

import java.security.MessageDigest;

public class MD5Encoder {
 
 public static String encode(String string) throws Exception {
  byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
  StringBuilder hex = new StringBuilder(hash.length * 2);
  for (byte b : hash) {
   if ((b & 0xFF) <0x10) {
    hex.append("0");
   }
   hex.append(Integer.toHexString(b & 0xFF));
  }
  return hex.toString();
 }
}

最后新建一个工具类来使用我们上面的三个缓存方法

/**
 * 三级缓存工具类
 * 
 * @author Ace
 * @date 2016-02-19
 */
public class MyBitmapUtils {

 // 网络缓存工具类
 private NetCacheUtils mNetUtils;
 // 本地缓存工具类
 private LocalCacheUtils mLocalUtils;
 // 内存缓存工具类
 private MemoryCacheUtils mMemoryUtils;

 public MyBitmapUtils() {
  mMemoryUtils = new MemoryCacheUtils();
  mLocalUtils = new LocalCacheUtils();
  mNetUtils = new NetCacheUtils(mLocalUtils, mMemoryUtils);
 }

 public void display(ImageView imageView, String url) {
  // 设置默认加载图片
  imageView.setImageResource(R.drawable.news_pic_default);

  // 先从内存缓存加载
  Bitmap bitmap = mMemoryUtils.getBitmapFromMemory(url);
  if (bitmap != null) {
   imageView.setImageBitmap(bitmap);
   System.out.println("从内存读取图片啦...");
   return;
  }

  // 再从本地缓存加载
  bitmap = mLocalUtils.getBitmapFromLocal(url);
  if (bitmap != null) {
   imageView.setImageBitmap(bitmap);
   System.out.println("从本地读取图片啦...");
   // 给内存设置图片
   mMemoryUtils.setBitmapToMemory(url, bitmap);
   return;
  }

  // 从网络缓存加载
  mNetUtils.getBitmapFromNet(imageView, url);
 }

}

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。


推荐阅读
  • Alibaba珍藏版mybatis手写文档,值得一读!
    一面问题:MySQLRedisKafka线程算法mysql知道哪些存储引擎,它们的区别mysql索引在什么情况下会失效mysql在项目中的优化场景&# ... [详细]
  • HashMap与LinkedHashMap的结构对比
    参考自:跳转HashMap和LinkedHashMap属于线程不安全的HashTable属于线程安全再来看看HashMap和LinkedHashMap的结构图 ... [详细]
  • Android使用系统相机拍照和读取相册照片1.拍照(对于7.0以上的版本,不在允许直接访问uri)`若不指定输出路径intent.putExtra(MediaStore.EXTR ... [详细]
  • 一文了解消息中间件RabbitMQ
    消息中间件---RabbitMQ1消息中间件的作用2.常用的消息中间件3消息中间件RabbitMQ3.1RabbitMQ介绍3.3RabbitMQ的队列模式3.3RabbitMQ的 ... [详细]
  • 我从GoogleFirebase服务中看过视频并阅读了Cloudfirestore的文档,但我无法想象实时数据库.我想到了这个Web应用程序,我希望从不同类别的产品中存储我的提供程 ... [详细]
  • Android休眠唤醒机制
    有四种方式可以引起休眠①在wake_unlock()中,如果发现解锁以后没有任何其他的wakelock了,就开始休眠②在定时器到时间以后,定时器的回调函 ... [详细]
  • .Net下RabbitMQ发布订阅模式实践
    一、概念AMQP,即AdvancedMessageQueuingProtocol,高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计。消息中间件主要用于组件之间的 ... [详细]
  • openstack安装和使用
    前言本篇文章会介绍openstack的安装、openstack各组件的配置和功能、还会演示openstack云主机的创建流程,整篇文章花费一天时间完成, ... [详细]
  • 点击:【第一章:Java开发岗:基础篇】计算机基础问题、HashMap、Fail-safe机制Fail-fast机制、Synchronized、ThreadLocal、AQS、线程 ... [详细]
  • 启动activemq_「Java」SpringBoot amp; ActiveMQ
    一、消息队列消息队列中间件是分布式系统中重要的组件,主要解决应用耦合、异步消息、流量削锋等问题,实现高性能、高可用、可伸缩和最终一致性架构, ... [详细]
  • Linux DNS
    libnss_files.solibnss_dnslibnss_ldap展现的就是一个配置文件etcnsswitch.conf?查看这个文件这个files就是通过libnss_fi ... [详细]
  • MySQL的内存结构与物理结构
    从MySQL的物理结构和内存结构开始了解整个MySQL的运行机制,其中有几个特别重要的概念,也是平时工作中更加关注的地方,如:binlog、redolog、表空间、慢查询日志、My ... [详细]
  • 德规自行车灯推荐(自行车灯品牌排行榜)
    日月如梭,斗转星移。随着太阳光顾南半球的时长逐渐增长,我们在能见度较低的情况下骑车的几率也越来越高。在这时,为了确保行车安全,也为了照亮前方的路,选择合适的车灯是十分必要的。小编根 ... [详细]
  • [剑指Offer笔记]3_数组数组,字符串:连续空间链表,树:大量指针操作栈:与递归相关队列:广度优先算法相关数组O(1)时间读写元素;可以用数组实现哈希表;有了哈希表可以实现O( ... [详细]
  • 说明:请用最少的代码算出以下问题!并贴出代码相互学习。1在一50人的队列中,从左至右报数,为单数者出列,其他 ... [详细]
author-avatar
塞上秋雪_838
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有