/* */ package com.superbearman6.imagecachetatics;
/* */
/* */ import android.content.Context;
/* */ import android.database.sqlite.SQLiteDatabase;
/* */ import android.graphics.Bitmap;
/* */ import android.util.Log;
/* */ import java.net.URL;
/* */ import java.util.List;
/* */ import java.util.Random;
/* */
/* */ public class ImageCacheManager
/* */ {
/* */ private DBClient dbClient;
/* 301 */ private static ImageCacheManager instance = null;
/* */ private Context mContext;
/* */ private FilesUtils filesUtils;
/* 307 */ private int mode = 1;
/* */
/* 309 */ private int max_Count = 20;
/* */
/* 311 */ private long delay_Millisecond = 259200000L;
/* */
/* 313 */ private long max_Memory = 3145728L;
/* */ public static final int MODE_LEAST_RECENTLY_USED = 0;
/* */ public static final int MODE_FIXED_TIMED_USED = 1;
/* */ public static final int MODE_FIXED_MEMORY_USED = 2;
/* */ public static final int MODE_NO_CACHE_USED = 3;
/* */
/* */ private ImageCacheManager(Context context, int mode, String tag)
/* */ {
/* 22 */ this.mode = mode;
/* 23 */ this.mContext = context;
/* 24 */ this.filesUtils = new FilesUtils(context);
/* 25 */ if (mode == 0)
/* 26 */ this.dbClient = new DBClient(this.mContext, "least_recently_used", tag);
/* 27 */ else if (mode == 1)
/* 28 */ this.dbClient = new DBClient(this.mContext, "fixed_timed_used", tag);
/* 29 */ else if (mode == 2)
/* 30 */ this.dbClient = new DBClient(this.mContext, "fixed_memory_used", tag);
/* */ }
/* */
/* */ public static synchronized ImageCacheManager getImageCacheService(Context context, int mode, String tag)
/* */ {
/* 45 */ if (instance == null) {
/* 46 */ instance = new ImageCacheManager(context, mode, tag);
/* */ }
/* 48 */ return instance;
/* */ }
/* */
/* */ public Bitmap downlaodImage(URL url) {
/* 52 */ Bitmap bitmap = null;
/* 53 */ CacheInfo cacheInfo = null;
/* 54 */ switch (this.mode)
/* */ {
/* */ case 0:
/* 56 */ cacheInfo = new LRU(url).execute();
/* 57 */ if (cacheInfo == null) break label133;
/* 58 */ bitmap = cacheInfo.getValue();
/* */
/* 60 */ break;
/* */ case 1:
/* 62 */ cacheInfo = new FTU(url).execute();
/* 63 */ if (cacheInfo == null) break label133;
/* 64 */ bitmap = cacheInfo.getValue();
/* */
/* 66 */ break;
/* */ case 2:
/* 68 */ cacheInfo = new FMU(url).execute();
/* 69 */ if (cacheInfo == null) break label133;
/* 70 */ bitmap = cacheInfo.getValue();
/* */
/* 72 */ break;
/* */ case 3:
/* 74 */ cacheInfo = this.filesUtils.downloadImage(url);
/* 75 */ if (cacheInfo == null) break label133;
/* 76 */ bitmap = cacheInfo.getValue();
/* */ }
/* */
/* 80 */ label133: return bitmap;
/* */ }
/* */
/* */ public void setMax_num(int max_num)
/* */ {
/* 316 */ this.max_Count = max_num;
/* */ }
/* */
/* */ public void setDelay_millisecond(long delay_millisecond) {
/* 320 */ this.delay_Millisecond = delay_millisecond;
/* */ }
/* */
/* */ public void setMax_Memory(long max_Memory) {
/* 324 */ this.max_Memory = max_Memory;
/* */ }
/* */
/* */ private class FMU
/* */ implements IDownload
/* */ {
/* 220 */ URL url = null;
/* */
/* */ FMU(URL url) {
/* 223 */ this.url = url;
/* */ }
/* */
/* */ public CacheInfo execute()
/* */ {
/* 228 */ SQLiteDatabase db = ImageCacheManager.this.dbClient.getSQLiteDatabase();
/* 229 */ db.beginTransaction();
/* 230 */ CacheInfo cacheInfo = null;
/* */ try {
/* 232 */ cacheInfo = ImageCacheManager.this.dbClient.select(this.url.toString(), db);
/* 233 */ List cacheInfos = ImageCacheManager.this.dbClient.selectAll(db);
/* 234 */ if (cacheInfo == null) {
/* 235 */ cacheInfo = ImageCacheManager.this.filesUtils.downloadImage(this.url);
/* 236 */ if (cacheInfo == null) {
/* */ return null;
/* */ }
/* 239 */ if (cacheInfo.getFileSize() > ImageCacheManager.this.max_Memory) {
/* 240 */ Log.i(ImageCacheManager.this.mContext.getPackageName(),
/* 241 */ "the image resource" +
/* 242 */ cacheInfo.getUrl().toString() +
/* 243 */ " need more storage than " +
/* 244 */ ImageCacheManager.this.max_Memory + "B");
/* */ } else {
/* 246 */ if ((cacheInfos != null) && (cacheInfos.size() > 0)) {
/* 247 */ long sumSize = 0L;
/* 248 */ while (cacheInfos.size() > 0) {
/* 249 */ int i = 0; for (int size = cacheInfos.size(); i < size; ++i) {
/* 250 */ CacheInfo tempCache = (CacheInfo)cacheInfos.get(i);
/* 251 */ sumSize += tempCache.getFileSize();
/* */ }
/* 253 */ if (sumSize + cacheInfo.getFileSize() <= ImageCacheManager.this.max_Memory) {
/* */ break;
/* */ }
/* 256 */ CacheInfo deleteCacheInfo = maxSize(cacheInfos);
/* 257 */ if (!ImageCacheManager.this.dbClient.delete(deleteCacheInfo.getUrl()
/* 258 */ .toString(), db)) continue;
/* 259 */ ImageCacheManager.this.filesUtils.deleteImage(deleteCacheInfo
/* 260 */ .getFileName());
/* 261 */ cacheInfos.remove(deleteCacheInfo);
/* */ }
/* */
/* */ }
/* */
/* 266 */ if (ImageCacheManager.this.dbClient.insert(cacheInfo, db))
/* 267 */ ImageCacheManager.this.filesUtils.saveImage(cacheInfo.getValue(),
/* 268 */ cacheInfo.getFileName());
/* */ }
/* */ }
/* */ else
/* */ {
/* 273 */ Bitmap bitmap = ImageCacheManager.this.filesUtils.readImage(cacheInfo
/* 274 */ .getFileName());
/* 275 */ if (bitmap != null)
/* 276 */ cacheInfo.setValue(bitmap);
/* */ }
/* */ }
/* */ finally
/* */ {
/* 281 */ db.endTransaction(); } db.endTransaction();
/* */
/* 283 */ return cacheInfo;
/* */ }
/* */
/* */ private CacheInfo maxSize(List<CacheInfo> cacheInfos) {
/* 287 */ long max = ((CacheInfo)cacheInfos.get(0)).getFileSize();
/* 288 */ CacheInfo deleteCache = (CacheInfo)cacheInfos.get(0);
/* 289 */ int i = 0; for (int size = cacheInfos.size(); i < size; ++i) {
/* 290 */ CacheInfo tempCache = (CacheInfo)cacheInfos.get(i);
/* 291 */ if (tempCache.getFileSize() > max) {
/* 292 */ deleteCache = tempCache;
/* */ }
/* */ }
/* 295 */ return deleteCache;
/* */ }
/* */ }
/* */
/* */ private class FTU
/* */ implements IDownload
/* */ {
/* 164 */ URL url = null;
/* */
/* */ FTU(URL url) {
/* 167 */ this.url = url;
/* */ }
/* */
/* */ public CacheInfo execute()
/* */ {
/* 172 */ CacheInfo cacheInfo = null;
/* 173 */ SQLiteDatabase db = ImageCacheManager.this.dbClient.getSQLiteDatabase();
/* 174 */ db.beginTransaction();
/* */ try {
/* 176 */ cacheInfo = ImageCacheManager.this.dbClient.select(this.url.toString(), db);
/* 177 */ if (cacheInfo == null) {
/* 178 */ cacheInfo =
没有合适的资源?快使用搜索试试~ 我知道了~
android缓存工具类源码
共5个文件
java:5个
4星 · 超过85%的资源 需积分: 9 10 下载量 109 浏览量
2013-05-24
21:24:53
上传
评论 2
收藏 7KB ZIP 举报
温馨提示
android 缓存 工具类 源码,为用户节省流量,把信息存放到数据库,三种缓存策略:(1)LRU算法,固定缓存图片数量(max_num),当图片数量超出max_num时,将缓存中最近用的最少的图片删除。 (2)FTU算法,固定每张图片的缓存时限,以最后一次使用算起,超过时限后删除。(3)FMU算法,在存储器中固定一定大小的存储空间,超过固定空间后 将缓存中占用最大尺寸的图片删除。使用时只需要向方法体中传递图片的URL即可。
资源推荐
资源详情
资源评论
收起资源包目录
imagecache.zip (5个子文件)
com
superbearman6
imagecachetatics
ImageCacheManager.java 13KB
CacheInfo.java 2KB
DBClient.java 6KB
IDownload.java 276B
FilesUtils.java 4KB
共 5 条
- 1
资源评论
- wj0372014-11-24具有参考价值,还是不错的。
LIJIEJIE00
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功