ImagesUtils 工具类Imagelolder
来源:互联网 发布:软件行业职位分类 编辑:程序博客网 时间:2024/06/01 19:23
package com.bawei.wzq.bitmapcachedemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* author:Created by WangZhiQiang on 2017-09-04.
* 处理三级缓存
* 内存缓存(LruCache-->最近最少使用算法,当缓存慢的时候,自动把最近使用最少的删除,腾出来的空间添加新的缓存内容)
* sd卡缓存
* 网络
*/
public class ImagesUtils {
Handler handler;
private File cacheDir;
private ExecutorService newFixedThreadPool;
private LruCache<String, Bitmap> lruCache;
/**
* @param context
* @param handler
*/
public ImagesUtils(Context context, Handler handler) {
//获得你手机上的最大内存
long maxMemory = Runtime.getRuntime().maxMemory();
int maxSize = (int) (maxMemory / 8);
this.handler = handler;
//得到本app在sd上的缓存文件夹
cacheDir = context.getCacheDir();
// 初始化线程池;初始化5个现成,供程序使用
newFixedThreadPool = Executors.newFixedThreadPool(5);
//内存缓存,底层用的最近最少算法;
lruCache = new LruCache<String, Bitmap>(maxSize) {
//每次缓存图片都要调用这个方法;
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
}
/**
* 取图片,
*
* @param path
* @return
*/
public Bitmap getBitMap(String path) {
//lrucache是管理内存缓存的;
Bitmap bitmap = lruCache.get(path);
if (bitmap != null) {
System.out.println("我走了内存");
return bitmap;
}
//从本直去取,sd卡去取bitmap
bitmap = getBitMapFromLocal(path);
if (bitmap != null) {
System.out.println("我走了本地缓存");
return bitmap;
}
// 从网络去取
getBitmapFromNet(path);
return null;
}
/**
* 从sd卡获取图片
*
* @param path
* @return
*/
private Bitmap getBitMapFromLocal(String path) {
try {
String encode = EncoderUtils.encode(path);
FileInputStream fileInputStream = new FileInputStream(cacheDir
+ "/" + encode);
Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
//存到内存
lruCache.put(path, bitmap);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 从网络
*
* @param path
*/
private void getBitmapFromNet(final String path) {
//用线程池里的线程执行请求网络操作;
newFixedThreadPool.execute(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Message msg = new Message();
msg.what = 111;
msg.obj = bitmap;
Bundle data = new Bundle();
data.putString("tag", path);
msg.setData(data);
handler.sendMessage(msg);
//缓存到本地
saveBitmapToLocal(bitmap, path);
//缓存到内存
lruCache.put(path, bitmap);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
protected void saveBitmapToLocal(Bitmap bitmap, String path) {
try {
String encode = EncoderUtils.encode(path);
FileOutputStream fileOutputStream = new FileOutputStream(cacheDir
+ "/" + encode);
//图片二次裁剪
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* author:Created by WangZhiQiang on 2017-09-04.
* 处理三级缓存
* 内存缓存(LruCache-->最近最少使用算法,当缓存慢的时候,自动把最近使用最少的删除,腾出来的空间添加新的缓存内容)
* sd卡缓存
* 网络
*/
public class ImagesUtils {
Handler handler;
private File cacheDir;
private ExecutorService newFixedThreadPool;
private LruCache<String, Bitmap> lruCache;
/**
* @param context
* @param handler
*/
public ImagesUtils(Context context, Handler handler) {
//获得你手机上的最大内存
long maxMemory = Runtime.getRuntime().maxMemory();
int maxSize = (int) (maxMemory / 8);
this.handler = handler;
//得到本app在sd上的缓存文件夹
cacheDir = context.getCacheDir();
// 初始化线程池;初始化5个现成,供程序使用
newFixedThreadPool = Executors.newFixedThreadPool(5);
//内存缓存,底层用的最近最少算法;
lruCache = new LruCache<String, Bitmap>(maxSize) {
//每次缓存图片都要调用这个方法;
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}
};
}
/**
* 取图片,
*
* @param path
* @return
*/
public Bitmap getBitMap(String path) {
//lrucache是管理内存缓存的;
Bitmap bitmap = lruCache.get(path);
if (bitmap != null) {
System.out.println("我走了内存");
return bitmap;
}
//从本直去取,sd卡去取bitmap
bitmap = getBitMapFromLocal(path);
if (bitmap != null) {
System.out.println("我走了本地缓存");
return bitmap;
}
// 从网络去取
getBitmapFromNet(path);
return null;
}
/**
* 从sd卡获取图片
*
* @param path
* @return
*/
private Bitmap getBitMapFromLocal(String path) {
try {
String encode = EncoderUtils.encode(path);
FileInputStream fileInputStream = new FileInputStream(cacheDir
+ "/" + encode);
Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
//存到内存
lruCache.put(path, bitmap);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 从网络
*
* @param path
*/
private void getBitmapFromNet(final String path) {
//用线程池里的线程执行请求网络操作;
newFixedThreadPool.execute(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Message msg = new Message();
msg.what = 111;
msg.obj = bitmap;
Bundle data = new Bundle();
data.putString("tag", path);
msg.setData(data);
handler.sendMessage(msg);
//缓存到本地
saveBitmapToLocal(bitmap, path);
//缓存到内存
lruCache.put(path, bitmap);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
protected void saveBitmapToLocal(Bitmap bitmap, String path) {
try {
String encode = EncoderUtils.encode(path);
FileOutputStream fileOutputStream = new FileOutputStream(cacheDir
+ "/" + encode);
//图片二次裁剪
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
阅读全文
0 0
- ImagesUtils 工具类Imagelolder
- ImageLolder使用
- ImagesUtils——三级缓存和二次采样
- 工具类
- 工具类!!!!
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- 工具类
- java,web应用中,关于多用户访问,是不是就是一个用户等于一个线程? 50 那一般的时候是多用户多线程还是多用户单线程。 6S根本停不下来 | 浏览 2065 次 我有更好的答案 发布于2016-
- 静态代理和动态代理的简单实现
- < 笔记 > JavaScript
- 季節問題
- java多线程之守护线程(Daemon)
- ImagesUtils 工具类Imagelolder
- 学习hibernate中的一些问题及知识(2)
- gStore 之 VS*-Tree
- 刷爆朋友圈的“可以喝的书”,你还记得吗?
- 从JDK源码角度看Float
- Windows下添加MySQL服务到系统服务以及注意事项
- 清除本地数据缓存
- 具有绝对定位的标签元素使用原生js获取其left,top的值
- unity3d实现3D物体上的点击事件