在Android加载网络图片一章中当滚动屏幕时会不但调用异步任务重新加载图片,这样即浪费网络资源用户体验也不好,那么我们可以将图片缓存起来,要显示时先从缓存获取.图片缓存的实现分为两种一种内存缓存,一种文件缓存.
内存缓存Google已经帮我们实现,通过LruCache你可以非常方便快速的实现内存缓存. 先简单了解下LruCache原理,Lru是最近最少使用的意思,要做内存缓存首先需要定义缓存空间大小,当缓存空间不够时,系统回收部份内存空间,回收依据是什么呢,这里采用的便是Lru算法,将最近最少使用的图片占用的内存空间进行回收.
内存缓存具体实现
初始化 LruCache
private LruCache<String, Bitmap> mMemoryCache;
// 获取应用程序最大可用内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
int cacheSize = maxMemory / 8;
// 设置图片缓存大小为程序最大可用内存的1/8
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getByteCount();
}
};
将一张图片存储到LruCache中
/**
* 将一张图片存储到LruCache中。
*
* @param key
* LruCache的键,这里传入图片的URL地址。
* @param bitmap
* LruCache的键,这里传入从网络上下载的Bitmap对象。
*/
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemoryCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
从LruCache中获取一张图片
/**
* 从LruCache中获取一张图片,如果不存在就返回null。
*
* @param key
* LruCache的键,这里传入图片的URL地址。
* @return 对应传入键的Bitmap对象,或者null。
*/
public Bitmap getBitmapFromMemoryCache(String key) {
return mMemoryCache.get(key);
}
文件缓存
内存空间毕竟是有限,当图片数量很大时,我们可以增加文件缓存. 文件缓存是将图片保存到文件中,当我们获取图片时首先从内存缓存中查找,如果不存在接着从文件缓存中查找,也不存在,再去网络获取。 文件缓存的实现同样有一个非常方便的第三方类DiskLruCache,现在很多APP都是通过它来实现. DiskLruCache的源码在Google Source上,地址如下: android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/libcore/io/DiskLruCache.java
实例化DiskLruCache打开缓存
DiskLruCache mDiskLruCache = null;
try {
File cacheDir = getDiskCacheDir(context, "bitmap");
if (!cacheDir.exists()) {
cacheDir.mkdirs();
}
mDiskLruCache = DiskLruCache.open(cacheDir, 1001, 1, 10 * 1024 * 1024);
} catch (IOException e) {
e.printStackTrace();
}
open()方法接收四个参数,第一个参数指定的是数据的缓存地址,第二个参数指定当前应用程序的版本号,第三个参数指定同一个key可以对应多少个缓存文件,基本都是传1,第四个参数指定最多可以缓存多少字节的数据。
获取缓存地址
public File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getExternalCacheDir().getPath();
} else {
cachePath = context.getCacheDir().getPath();
}
return new File(cachePath + File.separator + uniqueName);
}
获取当前应用程序版本号
public int getAppVersion(Context context) {
try {
PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return info.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
写图片到文件缓存 写入时不能直接使用图片Url做为key因为图片Url地址很可能存在不能使用的文件地址符号,先进行MD5编码
public String hashKeyForDisk(String key) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
private String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
先要得到DiskLruCache.Editor的实例
String imageUrl = "https://img-my.youkuaiyun.com/uploads/201309/01/1378037235_7476.jpg"; String key = hashKeyForDisk(imageUrl); DiskLruCache.Editor editor = mDiskLruCache.edit(key);
一次完整写入操作的代码如下所示
new Thread(new Runnable() {
@Override
public void run() {
try {
String imageUrl = "https://img-my.youkuaiyun.com/uploads/201309/01/1378037235_7476.jpg";
String key = hashKeyForDisk(imageUrl);
DiskLruCache.Editor editor = mDiskLruCache.edit(key);
if (editor != null) {
OutputStream outputStream = editor.newOutputStream(0);
//根据imageUrl从网络加载图片写入outPutStream
if (downloadUrlToStream(imageUrl, outputStream)) {
editor.commit();
} else {
editor.abort();
}
}
mDiskLruCache.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
读取缓存
try {
String imageUrl = "https://img-my.youkuaiyun.com/uploads/201309/01/1378037235_7476.jpg";
String key = hashKeyForDisk(imageUrl);
DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);
if (snapShot != null) {
InputStream is = snapShot.getInputStream(0);
Bitmap bitmap = BitmapFactory.decodeStream(is);
mImage.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
1016

被折叠的 条评论
为什么被折叠?



