三级缓存工具类直接引用
来源:互联网 发布:中国大数据发展历程 编辑:程序博客网 时间:2024/06/05 09:15
//福利超级简单好用
//工具类中引导的jar包可以自己下载
package com.sdp.panda.pictrueapp;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.LruCache;
import com.jakewharton.disklrucache.DiskLruCache;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Created by 80926 on 2016/10/19.
* 包涵内存和磁盘缓存
*/
//工具类:LruCacheUtils
public class LruCacheUtils {
private static LruCacheUtils instance;//单例模式
private LruCache<String, Bitmap> lruCache;
private Context context;
private DiskLruCache diskLruCache;
private LruCacheUtils() {}
public static LruCacheUtils getInstance() {
if (instance == null) {
instance = new LruCacheUtils();
}
return instance;
}
//打开磁盘缓存
public void open(Context context, String disk_cache_subdir, int dis_cache_size) {
try {
this.context = context;
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = manager.getMemoryClass();
lruCache = new LruCache<>((memoryClass / 8) * 1024 * 1024);//获得内存缓存空间为给定的内存的1/8,
/**
* getAppVersion():获取版本的时候,就会清除缓存
* 1:为一个key存多少个类型的缓存,磁盘的大小
* dis_cache_size:自己存储的大小,通常为10M
*/
diskLruCache = DiskLruCache.open(getCacheDir(disk_cache_subdir), getAppVersion(), 1, dis_cache_size);
} catch (IOException e) {
e.printStackTrace();
}
}
//缓存文件
private File getCacheDir(String name) {
//如果有sd卡,那么创建在外部储存mnt/android/data/packageame/cache/name ,
// 否则创建在内部储存dada/data/package/cache/name中(最好为1m)
String cachePath = (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED || !Environment.isExternalStorageRemovable() ?
context.getExternalCacheDir().getPath() : context.getCacheDir().getPath());
return new File(cachePath + File.separator + name);
}
//版本信息
private int getAppVersion() {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
//根据MD5计算出来的字符串,根据下载的地址转换成MD5的新字符串
public String hashKeyForDisk(String url){
String cacheKey;
try {
MessageDigest digest = MessageDigest.getInstance("MD5");//计算摘要 16进制的符号
digest.update(url.getBytes());
cacheKey = bytesToHexString(digest.digest());
}catch (NoSuchAlgorithmException e){
cacheKey = String.valueOf(url.hashCode());
}
return cacheKey;
}
public String bytesToHexString(byte[] digests){
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < digests.length ; i ++){
String hex = Integer.toHexString(0xFF&digests[i]);
if (hex.length() ==1){
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
/**
* 下载图片到内存和磁盘
* 使用到下载就需要异步任务完成
*/
public void fromNetToCache(String url, final int reqWidth, final int reqHeight, final Callback callback){
new AsyncTask<String,Void,Bitmap>(){
@Override
protected Bitmap doInBackground(String... params) {
String key = hashKeyForDisk(params[0]);
System.out.println("key:::::"+key);
DiskLruCache.Editor editor = null;
Bitmap bitmap = null;
try {
URL url = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(1000*30);
conn.setConnectTimeout(1000*30);
ByteArrayOutputStream baos = null;
if (conn.getResponseCode()==HttpURLConnection.HTTP_OK&&conn!=null){
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = bis.read(buffer))!=-1){
baos.write(buffer,0,len);
baos.flush();
}
bis.close();
baos.close();
conn.disconnect();
}
if (baos!=null){
bitmap = decodeSampleBitmapFromStream(baos.toByteArray(),reqWidth,reqHeight);//缩小以后的位图
addBitmapToCache(params[0],bitmap);//添加到缓存种
editor = diskLruCache.edit(key);//添加到磁盘
System.out.println(url.getFile());
//这个方法是将文件存在输出流当中,并且可以压缩,这里只是操作一张图片所以为 0
//这里的100为不压缩,70的话是压缩30%
bitmap.compress(Bitmap.CompressFormat.JPEG,100,editor.newOutputStream(0));
editor.commit();
}
}catch (IOException e){
try {
editor.abort();
}catch (IOException ie){
ie.printStackTrace();
}
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
callback.response(bitmap);
}
}.execute(url);
}
//从磁盘中取
public InputStream getDiskCache(final String url){
String key = hashKeyForDisk(url);
System.out.println("diskKey::::"+key);
try {
DiskLruCache.Snapshot snapshot = diskLruCache.get(key);
if (snapshot!=null){
InputStream is = snapshot.getInputStream(0);
return snapshot.getInputStream(0);
}
}catch (IOException e){
e.printStackTrace();
}
return null;
}
//关闭磁盘缓存的方法
public void close(){
if (diskLruCache!=null && !diskLruCache.isClosed()){
try {
diskLruCache.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//刷新的磁盘缓存的方法
public void flush() {
if (diskLruCache!=null){
try {
diskLruCache.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//回掉接口
public interface Callback<BitMap>{
void response(Bitmap entity);
}
/**
* 以下的是往内存中存储
*/
//计算所有变换的比例,采样率
public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
if (height>reqHeight||width>reqWidth){
if (width>height){
inSampleSize = Math.round((float)height/(float)reqHeight);
}else {
inSampleSize = Math.round((float)width/(float)reqWidth);
}
}
return inSampleSize;
}
//从网路中得到的字节流,得到需求的bitmap
public Bitmap decodeSampleBitmapFromStream(byte[] bytes,int reqWidth, int reqHeight){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//只是获取它的属性不显示图片
BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;//不获取属性,
return BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
}
//从本地图片中得到得到最新位图
public Bitmap decodeSampleBitmapFromResource(Resources resources, int resId, int reqWidth, int reqHeight){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//只是获取它的属性不显示图片
BitmapFactory.decodeResource(resources,resId,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;//不获取属性,
return BitmapFactory.decodeResource(resources,resId,options);
}
//LRU缓存,往内存中添加位图
public void addBitmapToCache(String url,Bitmap bitmap){
String key = hashKeyForDisk(url);
if (getBitmapFromCache(key)==null){
lruCache.put(key,bitmap);
}
}
//从LRU内存中取出要用的key对应的bitmap
public Bitmap getBitmapFromCache(String url){
String key = hashKeyForDisk(url);
return lruCache.get(key);
}
}
//对工具类的调用以用法
private String url = "http://www.xxx.xxx.png";
LruCacheUtils instance = LruCacheUtils.getInstance();//获取类
Button btn = (Button)findViewById(R.id.btn_test);
ImageView iv = (Button)findViewById(R.id.btn_test);
btn.setOnClickListener(new OnClickListener()){
@override
public void click(View v){
//300,200为想要的图片尺寸
loadBitmap(url,300,200);
}
};
private void loadBitmap(String url,int reqWidth,int reqHeight){
//1、先从内存中获取
Bitmap bitmap = instance.getBitmapFromCache();
if(bitmap==null){
//2、再从磁盘中获取
InputStream is = instance.getDiskCache(url);
if(is==null){
//3、最后从网路中下载并保存内存中
instance.fromNetToCache(url,,reqWidth,reqHeight,new LruCacheUtils.Callback() {
@Override
public void response(Bitmap entity) {
iv.setImageBitmap(entity);
}
});)
}else{
bitmap = BitmapFactory.decodeStream(is);
//需要将磁盘中保存的文件添加到内存中
instance.addBitmapToCache(url,bitmap);
iv.setImageBitmap(bitmap);
}
}else{
iv.setImageBitmap(bitmap);
}
}
//工具类中引导的jar包可以自己下载
package com.sdp.panda.pictrueapp;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.LruCache;
import com.jakewharton.disklrucache.DiskLruCache;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Created by 80926 on 2016/10/19.
* 包涵内存和磁盘缓存
*/
//工具类:LruCacheUtils
public class LruCacheUtils {
private static LruCacheUtils instance;//单例模式
private LruCache<String, Bitmap> lruCache;
private Context context;
private DiskLruCache diskLruCache;
private LruCacheUtils() {}
public static LruCacheUtils getInstance() {
if (instance == null) {
instance = new LruCacheUtils();
}
return instance;
}
//打开磁盘缓存
public void open(Context context, String disk_cache_subdir, int dis_cache_size) {
try {
this.context = context;
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = manager.getMemoryClass();
lruCache = new LruCache<>((memoryClass / 8) * 1024 * 1024);//获得内存缓存空间为给定的内存的1/8,
/**
* getAppVersion():获取版本的时候,就会清除缓存
* 1:为一个key存多少个类型的缓存,磁盘的大小
* dis_cache_size:自己存储的大小,通常为10M
*/
diskLruCache = DiskLruCache.open(getCacheDir(disk_cache_subdir), getAppVersion(), 1, dis_cache_size);
} catch (IOException e) {
e.printStackTrace();
}
}
//缓存文件
private File getCacheDir(String name) {
//如果有sd卡,那么创建在外部储存mnt/android/data/packageame/cache/name ,
// 否则创建在内部储存dada/data/package/cache/name中(最好为1m)
String cachePath = (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED || !Environment.isExternalStorageRemovable() ?
context.getExternalCacheDir().getPath() : context.getCacheDir().getPath());
return new File(cachePath + File.separator + name);
}
//版本信息
private int getAppVersion() {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 1;
}
//根据MD5计算出来的字符串,根据下载的地址转换成MD5的新字符串
public String hashKeyForDisk(String url){
String cacheKey;
try {
MessageDigest digest = MessageDigest.getInstance("MD5");//计算摘要 16进制的符号
digest.update(url.getBytes());
cacheKey = bytesToHexString(digest.digest());
}catch (NoSuchAlgorithmException e){
cacheKey = String.valueOf(url.hashCode());
}
return cacheKey;
}
public String bytesToHexString(byte[] digests){
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < digests.length ; i ++){
String hex = Integer.toHexString(0xFF&digests[i]);
if (hex.length() ==1){
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
/**
* 下载图片到内存和磁盘
* 使用到下载就需要异步任务完成
*/
public void fromNetToCache(String url, final int reqWidth, final int reqHeight, final Callback callback){
new AsyncTask<String,Void,Bitmap>(){
@Override
protected Bitmap doInBackground(String... params) {
String key = hashKeyForDisk(params[0]);
System.out.println("key:::::"+key);
DiskLruCache.Editor editor = null;
Bitmap bitmap = null;
try {
URL url = new URL(params[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(1000*30);
conn.setConnectTimeout(1000*30);
ByteArrayOutputStream baos = null;
if (conn.getResponseCode()==HttpURLConnection.HTTP_OK&&conn!=null){
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = bis.read(buffer))!=-1){
baos.write(buffer,0,len);
baos.flush();
}
bis.close();
baos.close();
conn.disconnect();
}
if (baos!=null){
bitmap = decodeSampleBitmapFromStream(baos.toByteArray(),reqWidth,reqHeight);//缩小以后的位图
addBitmapToCache(params[0],bitmap);//添加到缓存种
editor = diskLruCache.edit(key);//添加到磁盘
System.out.println(url.getFile());
//这个方法是将文件存在输出流当中,并且可以压缩,这里只是操作一张图片所以为 0
//这里的100为不压缩,70的话是压缩30%
bitmap.compress(Bitmap.CompressFormat.JPEG,100,editor.newOutputStream(0));
editor.commit();
}
}catch (IOException e){
try {
editor.abort();
}catch (IOException ie){
ie.printStackTrace();
}
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
callback.response(bitmap);
}
}.execute(url);
}
//从磁盘中取
public InputStream getDiskCache(final String url){
String key = hashKeyForDisk(url);
System.out.println("diskKey::::"+key);
try {
DiskLruCache.Snapshot snapshot = diskLruCache.get(key);
if (snapshot!=null){
InputStream is = snapshot.getInputStream(0);
return snapshot.getInputStream(0);
}
}catch (IOException e){
e.printStackTrace();
}
return null;
}
//关闭磁盘缓存的方法
public void close(){
if (diskLruCache!=null && !diskLruCache.isClosed()){
try {
diskLruCache.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//刷新的磁盘缓存的方法
public void flush() {
if (diskLruCache!=null){
try {
diskLruCache.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//回掉接口
public interface Callback<BitMap>{
void response(Bitmap entity);
}
/**
* 以下的是往内存中存储
*/
//计算所有变换的比例,采样率
public int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
if (height>reqHeight||width>reqWidth){
if (width>height){
inSampleSize = Math.round((float)height/(float)reqHeight);
}else {
inSampleSize = Math.round((float)width/(float)reqWidth);
}
}
return inSampleSize;
}
//从网路中得到的字节流,得到需求的bitmap
public Bitmap decodeSampleBitmapFromStream(byte[] bytes,int reqWidth, int reqHeight){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//只是获取它的属性不显示图片
BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;//不获取属性,
return BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
}
//从本地图片中得到得到最新位图
public Bitmap decodeSampleBitmapFromResource(Resources resources, int resId, int reqWidth, int reqHeight){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//只是获取它的属性不显示图片
BitmapFactory.decodeResource(resources,resId,options);
options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;//不获取属性,
return BitmapFactory.decodeResource(resources,resId,options);
}
//LRU缓存,往内存中添加位图
public void addBitmapToCache(String url,Bitmap bitmap){
String key = hashKeyForDisk(url);
if (getBitmapFromCache(key)==null){
lruCache.put(key,bitmap);
}
}
//从LRU内存中取出要用的key对应的bitmap
public Bitmap getBitmapFromCache(String url){
String key = hashKeyForDisk(url);
return lruCache.get(key);
}
}
//对工具类的调用以用法
private String url = "http://www.xxx.xxx.png";
LruCacheUtils instance = LruCacheUtils.getInstance();//获取类
Button btn = (Button)findViewById(R.id.btn_test);
ImageView iv = (Button)findViewById(R.id.btn_test);
btn.setOnClickListener(new OnClickListener()){
@override
public void click(View v){
//300,200为想要的图片尺寸
loadBitmap(url,300,200);
}
};
private void loadBitmap(String url,int reqWidth,int reqHeight){
//1、先从内存中获取
Bitmap bitmap = instance.getBitmapFromCache();
if(bitmap==null){
//2、再从磁盘中获取
InputStream is = instance.getDiskCache(url);
if(is==null){
//3、最后从网路中下载并保存内存中
instance.fromNetToCache(url,,reqWidth,reqHeight,new LruCacheUtils.Callback() {
@Override
public void response(Bitmap entity) {
iv.setImageBitmap(entity);
}
});)
}else{
bitmap = BitmapFactory.decodeStream(is);
//需要将磁盘中保存的文件添加到内存中
instance.addBitmapToCache(url,bitmap);
iv.setImageBitmap(bitmap);
}
}else{
iv.setImageBitmap(bitmap);
}
}
阅读全文
0 0
- 三级缓存工具类直接引用
- 三级缓存工具类
- 图片三级缓存工具类
- Android-网络图片下载工具类-三级缓存
- Android-网络图片下载工具类-三级缓存
- Android 图片三级缓存机制工具类封装
- Android 图片三级缓存机制工具类封装
- Volley框架下的三级缓存的使用工具类
- 【android】实现图片的三级缓存。工具类
- Android 图片三级缓存机制工具类封装
- Android 图片三级缓存机制工具类封装
- android 实现图片的三级缓存工具类
- 图片三级缓存工具类(基与LruCache和 DiskLruCache)
- 三级缓存之弱引用概念
- 三级缓存图片类
- 基与LruCache(内存缓存)和 DiskLruCache(硬盘缓存)的图片三级缓存工具类
- zhu的工作日记:图片三级缓存的工具类(android)
- 三级缓存
- 用g++编译程序时符号说明
- 22 《巴黎文学散步地图》 -豆瓣评分7.8
- SVN操作手册
- Spring事务总结---传播级别以及REQUIRED_NEW及NESTED的使用场景(赞)
- RxJava笔记——常用的一些操作符
- 三级缓存工具类直接引用
- 记录php中非常好用的兼容性换行符PHP_EOL
- 【QT】无边框窗口+背景图片+用户区可拖动
- 在Windows下删除了linux分区,开机时无法启动Windows开机引导
- java——String源码(一)属性和构造器
- 从端到云——工业物联网项目全栈快速开发
- 什么时候用抽象?什么时候用接口?
- H5App-美丽天气
- linux使用flock文件锁解决crontab冲突问题