实现图片缓存也不难,需要有相应的cache策略。这里我采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cache,这里姑且也把它划到缓存的层次结构中

1.简介 
现在android应用中不可避免的要使用图片,有些图片是可以变化的,需要每次启动时从网络拉取,这种场景在有广告位的应用以及纯图片应用(比如百度美拍)中比较多。

现在有一个问题:假如每次启动的时候都从网络拉取图片的话,势必会消耗很多流量。在当前的状况下,对于非wifi用户来说,流量还是很贵的,一个很耗流量的应用,其用户数量级肯定要受到影响。当然,我想,向百度美拍这样的应用,必然也有其内部的图片缓存策略。总之,图片缓存是很重要而且是必须的。

2.图片缓存的原理 
实现图片缓存也不难,需要有相应的cache策略。这里我采用 内存-文件-网络 三层cache机制,其中内存缓存包括强引用缓存和软引用缓存(SoftReference),其实网络不算cache,这里姑且也把它划到缓存的层次结构中。当根据url向网络拉取图片的时候,先从内存中找,如果内存中没有,再从缓存文件中查找,如果缓存文件中也没有,再从网络上通过http请求拉取图片。在键值对(key-value)中,这个图片缓存的key是图片url的hash值,value就是bitmap。所以,按照这个逻辑,只要一个url被下载过,其图片就被缓存起来了。

关于Java中对象的软引用(SoftReference),如果一个对象具有软引用,内存空间足够,垃 圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。软引用可用来实现内存敏感的高 速缓存。使用软引用能防止内存泄露,增强程序的健壮性。

从代码上来说,采用一个ImageManager来负责图片的管理和缓存,函数接口为public void loadBitmap(String url, Handler handler) ;其中url为要下载的图片地址,handler为图片下载成功后的回调,在handler中处理message,而message中包含了图片的信息以及bitmap对象。ImageManager中使用的ImageMemoryCache(内存缓存)、ImageFileCache(文件缓存)以及LruCache(最近最久未使用缓存)会在后续文章中介绍。

/* 
* 图片管理 
* 异步获取图片,直接调用loadImage()函数,该函数自己判断是从缓存还是网络加载 
* 同步获取图片,直接调用getBitmap()函数,该函数自己判断是从缓存还是网络加载 
* 仅从本地获取图片,调用getBitmapFromNative() 
* 仅从网络加载图片,调用getBitmapFromHttp() 

*/ 
public class ImageManager implements IManager 

private final static String TAG = "ImageManager";

private ImageMemoryCache imageMemoryCache; //内存缓存

private ImageFileCache imageFileCache; //文件缓存

//正在下载的image列表 
public static HashMap<String, Handler> ongoingTaskMap = new HashMap<String, Handler>();

//等待下载的image列表 
public static HashMap<String, Handler> waitingTaskMap = new HashMap<String, Handler>();

//同时下载图片的线程个数 
final static int MAX_DOWNLOAD_IMAGE_THREAD = 4;

private final Handler downloadStatusHandler = new Handler(){ 
public void handleMessage(Message msg) 

startDownloadNext(); 

};

public ImageManager() 

imageMemoryCache = new ImageMemoryCache(); 
imageFileCache = new ImageFileCache(); 
}

/** 
* 获取图片,多线程的入口 
*/ 
public void loadBitmap(String url, Handler handler) 

//先从内存缓存中获取,取到直接加载 
Bitmap bitmap = getBitmapFromNative(url); 
if (bitmap != null) 

Logger.d(TAG, "loadBitmap:loaded from native"); 
Message msg = Message.obtain(); 
Bundle bundle = new Bundle(); 
bundle.putString("url", url); 
msg.obj = bitmap; 
msg.setData(bundle); 
handler.sendMessage(msg); 

else 

Logger.d(TAG, "loadBitmap:will load by network"); 
downloadBmpOnNewThread(url, handler); 


/** 
* 新起线程下载图片 
*/ 
private void downloadBmpOnNewThread(final String url, final Handler handler) 

Logger.d(TAG, "ongoingTaskMap'size=" + ongoingTaskMap.size());

if (ongoingTaskMap.size() >= MAX_DOWNLOAD_IMAGE_THREAD) 

synchronized (waitingTaskMap) 

waitingTaskMap.put(url, handler); 


else 

synchronized (ongoingTaskMap) 

ongoingTaskMap.put(url, handler); 

new Thread() 

public void run() 

Bitmap bmp = getBitmapFromHttp(url); 
// 不论下载是否成功,都从下载队列中移除,再由业务逻辑判断是否重新下载 
// 下载图片使用了httpClientRequest,本身已经带了重连机制 
synchronized (ongoingTaskMap) 

ongoingTaskMap.remove(url); 
}

if(downloadStatusHandler != null) 

downloadStatusHandler.sendEmptyMessage(0);


Message msg = Message.obtain(); 
msg.obj = bmp; 
Bundle bundle = new Bundle(); 
bundle.putString("url", url); 
msg.setData(bundle);

if(handler != null) 

handler.sendMessage(msg); 


}.start(); 


/** 
* 依次从内存,缓存文件,网络上加载单个bitmap,不考虑线程的问题 
*/ 
public Bitmap getBitmap(String url) 

// 从内存缓存中获取图片 
Bitmap bitmap = imageMemoryCache.getBitmapFromMemory(url); 
if (bitmap == null) 

// 文件缓存中获取 
bitmap = imageFileCache.getImageFromFile(url); 
if (bitmap != null) 

// 添加到内存缓存 
imageMemoryCache.addBitmapToMemory(url, bitmap); 

else 

// 从网络获取 
bitmap = getBitmapFromHttp(url); 


return bitmap; 
}

/** 
* 从内存或者缓存文件中获取bitmap 
*/ 
public Bitmap getBitmapFromNative(String url) 

Bitmap bitmap = null; 
bitmap = imageMemoryCache.getBitmapFromMemory(url);

if(bitmap == null) 

bitmap = imageFileCache.getImageFromFile(url); 
if(bitmap != null) 

// 添加到内存缓存 
imageMemoryCache.addBitmapToMemory(url, bitmap); 


return bitmap; 
}

/** 
* 通过网络下载图片,与线程无关 
*/ 
public Bitmap getBitmapFromHttp(String url) 

Bitmap bmp = null;

try 

byte[] tmpPicByte = getImageBytes(url);

if (tmpPicByte != null) 

bmp = BitmapFactory.decodeByteArray(tmpPicByte, 0, 
tmpPicByte.length); 

tmpPicByte = null; 

catch(Exception e) 

e.printStackTrace(); 
}

if(bmp != null) 

// 添加到文件缓存 
imageFileCache.saveBitmapToFile(bmp, url); 
// 添加到内存缓存 
imageMemoryCache.addBitmapToMemory(url, bmp); 

return bmp; 
}

/** 
* 下载链接的图片资源 

* @param url 

* @return 图片 
*/ 
public byte[] getImageBytes(String url) 

byte[] pic = null; 
if (url != null && !"".equals(url)) 

Requester request = RequesterFactory.getRequester( 
Requester.REQUEST_REMOTE, RequesterFactory.IMPL_HC); 
// 执行请求 
MyResponse myResponse = null; 
MyRequest mMyRequest; 
mMyRequest = new MyRequest(); 
mMyRequest.setUrl(url); 
mMyRequest.addHeader(HttpHeader.REQ.ACCEPT_ENCODING, "identity"); 
InputStream is = null; 
ByteArrayOutputStream baos = null; 
try { 
myResponse = request.execute(mMyRequest); 
is = myResponse.getInputStream().getImpl(); 
baos = new ByteArrayOutputStream(); 
byte[] b = new byte[512]; 
int len = 0; 
while ((len = is.read(b)) != -1) 

baos.write(b, 0, len); 
baos.flush(); 

pic = baos.toByteArray(); 
Logger.d(TAG, "icon bytes.length=" + pic.length); 

catch (Exception e3) 

e3.printStackTrace(); 
try 

Logger.e(TAG, 
"download shortcut icon faild and responsecode=" 
+ myResponse.getStatusCode()); 

catch (Exception e4) 

e4.printStackTrace(); 


finally 

try 

if (is != null) 

is.close(); 
is = null; 


catch (Exception e2) 

e2.printStackTrace(); 

try 

if (baos != null) 

baos.close(); 
baos = null; 


catch (Exception e2) 

e2.printStackTrace(); 

try 

request.close(); 

catch (Exception e1) 

e1.printStackTrace(); 



return pic; 
}

/** 
* 取出等待队列第一个任务,开始下载 
*/ 
private void startDownloadNext() 

synchronized(waitingTaskMap) 

Logger.d(TAG, "begin start next"); 
Iterator iter = waitingTaskMap.entrySet().iterator();

while (iter.hasNext()) 
{

Map.Entry entry = (Map.Entry) iter.next(); 
Logger.d(TAG, "WaitingTaskMap isn't null,url=" + (String)entry.getKey());

if(entry != null) 

waitingTaskMap.remove(entry.getKey()); 
downloadBmpOnNewThread((String)entry.getKey(), (Handler)entry.getValue()); 

break; 


}

public String startDownloadNext_ForUnitTest() 

String urlString = null; 
synchronized(waitingTaskMap) 

Logger.d(TAG, "begin start next"); 
Iterator iter = waitingTaskMap.entrySet().iterator();

while (iter.hasNext()) 

Map.Entry entry = (Map.Entry) iter.next(); 
urlString = (String)entry.getKey(); 
waitingTaskMap.remove(entry.getKey()); 
break; 


return urlString; 
}

/** 
* 图片变为圆角 
* @param bitmap:传入的bitmap 
* @param pixels:圆角的度数,值越大,圆角越大 
* @return bitmap:加入圆角的bitmap 
*/ 
public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) 

if(bitmap == null) 
return null; 
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
Canvas canvas = new Canvas(output); 
final int color = 0xff424242; 
final Paint paint = new Paint(); 
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
final RectF rectF = new RectF(rect); 
final float roundPx = pixels; 
paint.setAntiAlias(true); 
canvas.drawARGB(0, 0, 0, 0); 
paint.setColor(color); 
canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
canvas.drawBitmap(bitmap, rect, rect, paint); 
return output; 
}

public byte managerId() 

return IMAGE_ID; 

}

Android加载图片的策略的更多相关文章

  1. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  2. 图片--Android加载图片导致内存溢出(Out of Memory异常)

    Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory  Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...

  3. Android加载图片OOM错误解决方式

    前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...

  4. android 加载图片oom若干方案小结

    本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结. 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定).一般我们可以通过获取当 ...

  5. android 加载图片框架--Glide使用详解

    一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的google I/O大会上发布的官方app.(PS:众所 ...

  6. Android加载图片导致内存溢出(Out of Memory异常)

    Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory  Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...

  7. android 加载图片防止内存溢出

    图片资源: private int fore[]; private int back[]; fore = new int[]{R.drawable.a0, R.drawable.a1, R.drawa ...

  8. 解决android加载图片时内存溢出问题

    尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完成decode后,最终都是通过jav ...

  9. Android加载图片小结

    应用中用到图片加载需要解决的问题 无网络环境下图片不可用 图片的本地缓存,或者默认预加载的图片 低配置机型,加载图像资源超内存(OutOfMemory, OoM) 需要合理使用内存,尤其是bitmap ...

随机推荐

  1. [Shell]sed命令在MAC和Linux下的不同使用方式

    ---------------------------------------------------------------------------------------------------- ...

  2. java字符流操作flush()方法及其注意事项

    java字符流操作flush()方法及其注意事项   flush()方法介绍 查阅文档可以发现,IO流中每一个类都实现了Closeable接口,它们进行资源操作之后都需要执行close()方法将流关闭 ...

  3. 基础编程复习:输出n以内的所有素数

    暴力遍历:对于1~n以内的每一数i 每一个i只需要考虑2~i开根号以内是否有可以让i整除的数,即(i%x==0)只要满足就不是素数 否则输出 #include<iostream> #inc ...

  4. [疑难杂症]__当你的Cortana搜索无法使用,显示纯白界面(ps:已解决).

    前言 这个问题是在前不久解决关于我电脑点击屏幕上方快捷方式不久后出现的问题,之前并没有出现过这样的错误,但因为使用到的情况比较少,就一直没有去解决,但在一点时间后,发现没有Cortana搜索栏还是十分 ...

  5. 【原创】C++之自定义高效的swap(1)

    1 问题背景     当交换两个包含了指针成员的类,我们最想看到的是直接交换其指针.但是当我们调用std::swap标准库这个模板函数时,通常它都会复制3个指针指向的对象作为交换所用,缺乏效率.如下: ...

  6. 关于kubernetes使用私有仓库一点说明

    一.概述 关于kubernetes使用私有docker image  registry的一些说明: 1.对于自己构建的项目镜像或一些不想暴露到外网的image需要使用自建的私有仓库,一般有两种选择:d ...

  7. Android Handler 机制总结

    写 Handler 原理的文章很多,就不重复写了,写不出啥新花样.这篇文章的主要是对 handler 原理的总结. 1.Android消息机制是什么? Android消息机制 主要指 Handler ...

  8. μC/OS-II 任务堆栈的初始化

    任务堆栈的作用 应用程序在创建一个新任务的时候,必须把在系统启动这个任务时 CPU 各寄存器所需要的初始数据(任务指针.任务堆栈指针.程序状态字等等),事先存放在任务的堆栈中,以备任务切换等操作时调用 ...

  9. NLP入门(五)用深度学习实现命名实体识别(NER)

    前言   在文章:NLP入门(四)命名实体识别(NER)中,笔者介绍了两个实现命名实体识别的工具--NLTK和Stanford NLP.在本文中,我们将会学习到如何使用深度学习工具来自己一步步地实现N ...

  10. Linux服务器GlashFish的Domain管理

    1.本文内容 Glassfish(V3.1)的Domain创建,删除,登陆,部署等 Linux客户端工具: SecureCRTPortable和 WinSCP (请自行下载) 2.1 新建Domain ...