Android图片二级缓存
想起刚開始写代码的时候,领导叫我写一个头像下载的方法,当时屁颠屁颠就写了一个图片下载的,每次都要去网络上请求,最后直接被pass掉了
当时的思路是这种
后来渐渐地就知道了有二级缓存这东西。
自己也阅读过非常多关于双缓存的文章。
APP开发到越后面。对性能的要求越高。那么双缓存的优势就逐渐体现出来了。
所谓图片双缓存。首先到执行内存中请求,再到sd卡请求,最后到网络请求,流程图例如以下
那我们从第一部開始解析
1.先看 内存缓存的代码
- public class MemoryCache implements ImageCache {
- private static final String TAG = MemoryCache.class.getSimpleName();
- private LruCache<String,Bitmap> mMemoryCache;
- public MemoryCache(){
- init();
- }
- private void init(){
- final int maxMemory = (int) (Runtime.getRuntime().maxMemory()/1024);
- final int cacheSize = maxMemory/4;
- mMemoryCache = new LruCache<String,Bitmap>(cacheSize){
- @Override
- protected int sizeOf(String key, Bitmap value) {
- return value.getRowBytes()*value.getHeight()/1024;
- }
- };
- }
- @Override
- public Bitmap get(String key) {
- Bitmap bitmap = mMemoryCache.get(key);
- if (bitmap!=null){
- Log.i(TAG,"File is exist in memory");
- }
- return mMemoryCache.get(key);
- }
- @Override
- public void put(String key, Bitmap bitmap) {
- if (get(key)==null) {
- mMemoryCache.put(key, bitmap);
- }
- }
- }
- private void init()
init()方法中对一些变量进行初始化,mMemoryCache用于在内存中缓存图片
- public Bitmap get(String key) {}
get()方法用于从内存中获得缓存
- public void put(String key, Bitmap bitmap) {}
put()方法将下载好的图片缓存到内存中,方便下次使用
2.再看sd卡缓存
- public class DiskCache implements ImageCache {
- private static final String TAG = DiskCache.class.getSimpleName();
- static String mPath ;
- public DiskCache(Context context){
- init(context);
- }
- private void init(Context context){
- // 获取图片缓存路径
- mPath = getDiskCachePath(context,"bitmap");
- File cacheDir = new File(mPath);
- if (!cacheDir.exists()) {
- cacheDir.mkdirs();
- }
- }
- @Override
- public Bitmap get(String key) {
- File file = new File(mPath+key);
- if (file.exists()){
- return BitmapFactory.decodeFile(mPath+key);
- }
- return null;
- }
- @Override
- public void put(String key, Bitmap bitmap) {
- FileOutputStream fileOutputStream = null;
- try {
- File file = new File(mPath+key);
- if (file.exists()){
- Log.i(TAG,"File is exist on disk");
- }
- fileOutputStream = new FileOutputStream(mPath+key);
- bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }finally {
- CloseUtils.closeQuietly(fileOutputStream);
- }
- }
- /**
- * 依据传入的dir获得路径
- * @param context
- * @param dir
- * @return
- */
- public String getDiskCachePath(Context context, String dir) {
- String cachePath;
- if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
- || !Environment.isExternalStorageRemovable()) {
- cachePath = context.getExternalCacheDir().getPath();
- } else {
- cachePath = context.getCacheDir().getPath();
- }
- return cachePath + File.separator + dir;
- }
- }
相同
- private void init()
init()方法中对一些变量进行初始化,mMemoryCache用于在内存中缓存图片
- public Bitmap get(String key) {}
get()方法用于从内存中获得缓存
- public void put(String key, Bitmap bitmap) {}
put()方法将下载好的图片缓存到内存中,方便下次使用
接下来我们会在一个叫DoubleCache的类中对以上两种缓存方式进行管理
- public class DoubleCache implements ImageCache {
- private static final String TAG = DoubleCache.class.getSimpleName();
- private MemoryCache mMemoryCache = null;
- private DiskCache mDiskCache = null;
- public DoubleCache(Context context){
- mMemoryCache = new MemoryCache();
- mDiskCache = new DiskCache(context);
- }
- @Override
- public Bitmap get(String url) {
- String key = url2Key(url);
- Bitmap bitmap = mMemoryCache.get(key);
- if(bitmap==null){
- bitmap = mDiskCache.get(key);
- }else {
- }
- return bitmap;
- }
- @Override
- public void put(String url, Bitmap bitmap) {
- String key = url2Key(url);
- mMemoryCache.put(key,bitmap);
- mDiskCache.put(key,bitmap);
- }
- //url转key
- private String url2Key(String url){
- String key = MD5.hashKeyForDisk(url)+".jpg";
- return key;
- }
- }
我们在获取缓存的时候先从内存中获取。当内存中击中直接返回,当内存中没有击中,则訪问sd卡。
3.看到这里,小伙伴们一定急了,这仅仅有从缓存中和sd卡中取图片,并没有从网络获取,别急。立即就来
- public class ImageLoader {
- private static final String TAG = ImageLoader.class.getSimpleName();
- private static ImageLoader sInstance;
- private DoubleCache mDoubleCache = null;
- private ExecutorService mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
- private ImageLoader(Context context) {
- mDoubleCache = new DoubleCache(context);
- }
- public static ImageLoader getInstance(Context context) {
- if (sInstance == null) {
- synchronized (ImageLoader.class) {
- sInstance = new ImageLoader(context);
- }
- }
- return sInstance;
- }
- public void displayImage(String url, ImageView imageView) {
- Bitmap bitmap = mDoubleCache.get(url);
- if (bitmap != null) {
- imageView.setImageBitmap(bitmap);
- mDoubleCache.put(url,bitmap);
- return;
- }
- submitLoadRequest(url, imageView);
- }
- private void submitLoadRequest(final String url, final ImageView imageView) {
- Log.i(TAG,"Download,url:"+url);
- imageView.setTag(url);
- mExecutorService.submit(new Runnable() {
- @Override
- public void run() {
- final Bitmap bitmap = downloadImage(url);
- if (imageView.getTag().equals(url)) {
- imageView.post(new Runnable() {
- @Override
- public void run() {
- imageView.setImageBitmap(bitmap);
- }
- });
- }
- mDoubleCache.put(url, bitmap);
- }
- });
- }
- Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg) {
- }
- };
- public Bitmap downloadImage(String url) {
- Bitmap bitmap = null;
- HttpURLConnection conn = null;
- try {
- URL url1 = new URL(url);
- conn = (HttpURLConnection) url1.openConnection();
- bitmap = BitmapFactory.decodeStream(conn.getInputStream());
- if (bitmap!=null){
- mDoubleCache.put(url,bitmap);
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (conn != null) {
- conn.disconnect();
- }
- }
- return bitmap;
- }
从
- displayImage()
方法中能够看到。假设缓存中都没有才从网络中获取
- public Bitmap downloadImage(String url) {}
下载完毕之后。把图片放到缓存中。
到这里,我们的第二张流程图就走完了。是不是非常easy。
我们在看一下是怎样使用的
- private ImageView imageView;
- private ImageView imageView2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- imageView = (ImageView) findViewById(R.id.image);
- imageView2 = (ImageView) findViewById(R.id.image2);
- imageView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ImageLoader.getInstance(MainActivity.this).displayImage("http://img.my.csdn.net/uploads/201407/26/1406383299_1976.jpg", imageView);
- }
- });
- imageView2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- ImageLoader.getInstance(MainActivity.this).displayImage("http://img.my.csdn.net/uploads/201407/26/1406383299_1976.jpg", imageView2);
- }
- });
- }
Android图片二级缓存的更多相关文章
- picasso_强大的Android图片下载缓存库
tag: android pic skill date: 2016/07/09 title: picasso-强大的Android图片下载缓存库 [本文转载自:泡在网上的日子 参考:http://bl ...
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选
毕加索的艺术--Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选 官网: http://square.github.i ...
- 【第五篇】Volley代码修改之图片二级缓存以及相关源码阅读(重写ImageLoader.ImageCache)
前面http://www.cnblogs.com/androidsuperman/p/8a157b18ede85caa61ca5bc04bba43d0.html 有讲到使用LRU来处理缓存的,但是只是 ...
- picasso-强大的Android图片下载缓存库
编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! pica ...
- Android 图片三级缓存之内存缓存(告别软引用(SoftRefrerence)和弱引用(WeakReference))
因为之前项目同事使用了图片三级缓存,今天整理项目的时候发现同事还是使用了软引用(SoftRefrerence)和弱引用(WeakReference),来管理在内存中的缓存.看到这个我就感觉不对了.脑海 ...
- Android 图片三级缓存
图片缓存的原理 实现图片缓存也不难,需要有相应的cache策略.这里采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cac ...
- android图片的缓存--节约内存提高程序效率
如今android应用占内存一个比一个大,android程序的质量亟待提高. 这里简单说说网络图片的缓存,我这边就简单的说说思路 1:网络图片,无疑须要去下载图片,我们不须要每次都去下载. 维护一张表 ...
- 如何使用picasso 对Android图片下载缓存
相比较其他,picasso的图片缓存更加简单一些,他只需要一行代码就可以表述:导入相关jar包 Picasso.with(context).load("图片路径").into(Im ...
- Android图片载入缓存框架Glide
Glide开源框架是Google推荐的图片载入和缓框架,其在Github上的开源地址是:https://github.com/bumptech/glide 当然一个Google推荐的框架肯定就是Vol ...
随机推荐
- 转载:ArcEngine二次开发界面基本设置
转自:https://blog.csdn.net/weixin_42032107/article/details/80644991 1. 在form窗体中添加菜单栏和状态栏控件 2. 添加li ...
- 折腾mysql的小坑记录
1.安装 CentOS下先卸载自带的mariadb rpm -qa | grep mariadb mariadb-libs--.el7_2.x86_64 mariadb--.el7_2.x86_64 ...
- 定制你自己的CRF模型
如何训练自己的CRF模型 Ansj从5.0版本之后支持训练自己的crf模型,在此之前虽然已经很早支持了CRF模型,但是在用户的自训练上要求苛刻,而且本身实现的CRF从性能到准确率都不如CRF++或者第 ...
- [转]create a basic sql server 2005 trigger to send email alerts
本文转自:http://blog.netnerds.net/2008/02/create-a-basic-sql-server-2005-trigger-to-send-e-mail-alerts/ ...
- 线程局部存储(TLS)
线程局部存储(TLS) 2011-10-11 09:59:28| 分类: Win32---API | 标签:tls |举报 |字号 订阅 什么是线程局部存储 众所周知,线程是执行的单元,同 ...
- 可以ping通虚拟机但不能telnet 9000端口
突然发现eclipse不能连上虚拟机了,报错是本机连接不上9000的端口. 觉得有点奇怪,就在命令行里试图ping通虚拟机,成功:但尝试这telnet 9000端口的时候,却报错连接不上. 上网查了这 ...
- Android轻量级的开源缓存框架ASimpleCache
点击查看原文 先上方法调用,写最经常使用的.其它不一一写 保存数据: ACache mACache=ACache.get(this); mACache.put("数据名称", js ...
- JBPM5流程设计器jbpm-designer-2.4.0.Final-tomcat.war的部署没法访问的问题
转自:http://blog.csdn.net/steveguoshao/article/details/8840607 在http://sourceforge.net/projects/jbpm/f ...
- Java实现MySQL图片存取操作
转载自:http://blog.csdn.net/thc1987/article/details/3972201 存入操作 /* ---------------表结构------------ 表名:s ...
- maven镜像制作
故事背景: 需要在客户现场部署测试demo,由于部署环境不提供外网环境,应用所需依赖无法通过外网下载. 初步的解决方案,在本地起一个maven的容器,将代码copy到maven的容器内,进行打包操作, ...