Android 缓存机制(二)
来源:互联网 发布:ubuntu怎么安装wps 编辑:程序博客网 时间:2024/05/23 01:58
外部文件缓存
[html] view plaincopy
private File mCacheDir = context.getCacheDir();
private static final int MAX_CACHE_SIZE = 20 * 1024 * 1024; //20M
private final LruCache<String, Long> sFileCache = new LruCache<String, Long>(MAX_CACHE_SIZE){
@Override
public int sizeOf(String key, Long value){
return value.intValue();
}
@Override
protected void entryRemoved(boolean evicted, String key, Long oldValue, Long newValue){
File file = getFile(key);
if(file != null)
file.delete();
}
}
private File getFile(String fileName) throws FileNotFoundException {
File file = new File(mCacheDir, fileName);
if(!file.exists() || !file.isFile())
throw new FileNotFoundException("文件不存在或有同名文件夹");
return file;
}
//缓存bitmap到外部存储
public boolean putBitmap(String key, Bitmap bitmap){
File file = getFile(key);
if(file != null){
Log.v("tag", "文件已经存在");
return true;
}
FileOutputStream fos = getOutputStream(key);
boolean saved = bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
if(saved){
synchronized(sFileCache){
sFileCache.put(key, getFile(key).length());
}
return true;
}
return false;
}
//根据key获取OutputStream
private FileOutputStream getOutputStream(String key){
if(mCacheDir == null)
return null;
FileOutputStream fos = new FileOutputStream(mCacheDir.getAbsolutePath() + File.separator + key);
return fos;
}
//获取bitmap
private static BitmapFactory.Options sBitmapOptions;
static {
sBitmapOptions = new BitmapFactory.Options();
sBitmapOptions.inPurgeable=true; //bitmap can be purged to disk
}
public Bitmap getBitmap(String key){
File bitmapFile = getFile(key);
if(bitmapFile != null){
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(bitmapFile), null, sBitmapOptions);
if(bitmap != null){
//重新将其缓存至硬引用中
...
}
}
}
[html] view plaincopy
private File mCacheDir = context.getCacheDir();
private static final int MAX_CACHE_SIZE = 20 * 1024 * 1024; //20M
private final LruCache<String, Long> sFileCache = new LruCache<String, Long>(MAX_CACHE_SIZE){
@Override
public int sizeOf(String key, Long value){
return value.intValue();
}
@Override
protected void entryRemoved(boolean evicted, String key, Long oldValue, Long newValue){
File file = getFile(key);
if(file != null)
file.delete();
}
}
private File getFile(String fileName) throws FileNotFoundException {
File file = new File(mCacheDir, fileName);
if(!file.exists() || !file.isFile())
throw new FileNotFoundException("文件不存在或有同名文件夹");
return file;
}
//缓存bitmap到外部存储
public boolean putBitmap(String key, Bitmap bitmap){
File file = getFile(key);
if(file != null){
Log.v("tag", "文件已经存在");
return true;
}
FileOutputStream fos = getOutputStream(key);
boolean saved = bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
if(saved){
synchronized(sFileCache){
sFileCache.put(key, getFile(key).length());
}
return true;
}
return false;
}
//根据key获取OutputStream
private FileOutputStream getOutputStream(String key){
if(mCacheDir == null)
return null;
FileOutputStream fos = new FileOutputStream(mCacheDir.getAbsolutePath() + File.separator + key);
return fos;
}
//获取bitmap
private static BitmapFactory.Options sBitmapOptions;
static {
sBitmapOptions = new BitmapFactory.Options();
sBitmapOptions.inPurgeable=true; //bitmap can be purged to disk
}
public Bitmap getBitmap(String key){
File bitmapFile = getFile(key);
if(bitmapFile != null){
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(bitmapFile), null, sBitmapOptions);
if(bitmap != null){
//重新将其缓存至硬引用中
...
}
}
}
0 0
- Android 缓存机制(二)
- Android 缓存机制(二)
- hibernate缓存机制(二)
- iOS缓存机制(二)
- iOS缓存机制(二)
- hibernate缓存机制(二)
- Android 缓存机制(一)
- Android 缓存机制(三)
- Android 缓存机制(一)
- 浏览器缓存机制浅析(二)
- 缓存机制 Cache ARC算法(二)
- Mybatis的缓存机制(二)
- Hibernate二缓存机制
- iOS开发之缓存(二):本地缓存机制
- 第十一章 缓存机制(二) Realm 缓存
- Android 缓存机制代码(软引用)
- Android图片缓存机制(Caching Bitmaps)
- android缓存机制
- 寻找WindowsFormsHost的苦难历程
- 4、快速排序
- PythonOCC 教程1: 安装
- 第三章-使用字符串——python基础教程(第二版)笔记
- [LeetCode] Palindrome Number 解题报告
- Android 缓存机制(二)
- 微信小程序,不可不知的一二三四
- 第三方开源库:流式布局:FlowLayout
- 开源PLM软件Aras详解七 在Aras的Method中如何引用外部DLL
- .cpp调用.c的变量或函数【转】
- 如何判断两个可移动磁盘卷是否在同一个USB HUB上?
- 生产者消费者问题
- Java算法基础之快速排序算法
- ubuntu 14.04 安装虚拟机12.1.0