package animtest.com.example.e531.unit7_piccache_demo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.util.LruCache;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.util.LruCache;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
/**
* Created by e531 on 2017/10/9.
*/
public class ImageCacheUtil {
* Created by e531 on 2017/10/9.
*/
public class ImageCacheUtil {
//内存缓存 基于linkedHaspMap key是图片的url地址
private LruCache<String,Bitmap> mLruCache;
private LruCache<String,Bitmap> mLruCache;
public ImageCacheUtil(Context context){
//得到当前应用程序的内存空间大小
int maxMemory= (int) Runtime.getRuntime().maxMemory();
int cacheSize=maxMemory/4;
int maxMemory= (int) Runtime.getRuntime().maxMemory();
int cacheSize=maxMemory/4;
//进行初使化,需要指定缓存空间大小
mLruCache=new LruCache<String, Bitmap>(cacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();//自定义返回的数据的大小
}
};
}
mLruCache=new LruCache<String, Bitmap>(cacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();//自定义返回的数据的大小
}
};
}
/**
* 根据key得到图片
* @param key 是图片的url地址
* @return
*/
public Bitmap getPicFromMemory(String key){
Bitmap bitmap=null;
try {
//1.进行md5加密处理
String imgKey=encode(key);
//2.直接获取数据
bitmap=mLruCache.get(imgKey);
* 根据key得到图片
* @param key 是图片的url地址
* @return
*/
public Bitmap getPicFromMemory(String key){
Bitmap bitmap=null;
try {
//1.进行md5加密处理
String imgKey=encode(key);
//2.直接获取数据
bitmap=mLruCache.get(imgKey);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
e.printStackTrace();
}
return bitmap;
}
/**
* 保存一张图片到内存缓存
* @param key
* @param bitmap
*/
public void savePicToMemory(String key,Bitmap bitmap){
* 保存一张图片到内存缓存
* @param key
* @param bitmap
*/
public void savePicToMemory(String key,Bitmap bitmap){
try {
//1.进行md5加密
String imgKey=encode(key);
//2.通过key得到editor对象
mLruCache.put(imgKey,bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
//1.进行md5加密
String imgKey=encode(key);
//2.通过key得到editor对象
mLruCache.put(imgKey,bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 使用缓存加载图片
* @param imgUrl
* @param img
*/
public void loadPic(String imgUrl,ImageView img){
* 使用缓存加载图片
* @param imgUrl
* @param img
*/
public void loadPic(String imgUrl,ImageView img){
//1.从内存缓存获取
Bitmap bitmap=getPicFromMemory(imgUrl);
if(bitmap!=null){
Log.d("zzz","从内存缓存中获取");
//直接进行显示
img.setImageBitmap(bitmap);
return;
}
Bitmap bitmap=getPicFromMemory(imgUrl);
if(bitmap!=null){
Log.d("zzz","从内存缓存中获取");
//直接进行显示
img.setImageBitmap(bitmap);
return;
}
//3.从网络获取
loadPicFromNet(imgUrl,img);
}
loadPicFromNet(imgUrl,img);
}
/**
* 从网络上下载图片
* @param url
* @param imageView
*/
public void loadPicFromNet(final String imgUrl, final ImageView imageView){
AsyncTask<Void,Void,Bitmap> mytask=new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap=null;
try {
URL url=new URL(imgUrl);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
* 从网络上下载图片
* @param url
* @param imageView
*/
public void loadPicFromNet(final String imgUrl, final ImageView imageView){
AsyncTask<Void,Void,Bitmap> mytask=new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... params) {
Bitmap bitmap=null;
try {
URL url=new URL(imgUrl);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
bitmap= BitmapFactory.decodeStream(inputStream);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
InputStream inputStream=connection.getInputStream();
bitmap= BitmapFactory.decodeStream(inputStream);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(bitmap!=null){
Log.d("zzz","从网络中获取");
//保存到内存缓存
savePicToMemory(imgUrl,bitmap);
Log.d("zzz","从网络中获取");
//保存到内存缓存
savePicToMemory(imgUrl,bitmap);
//进行显示
imageView.setImageBitmap(bitmap);
}
}
};
mytask.execute();
}
imageView.setImageBitmap(bitmap);
}
}
};
mytask.execute();
}
/**
* 把一个字符串以md5的放肆加密之后返回...
* 因为url路径里面可能存在一些不可用的特殊字符,所以使用这种方式处理一下
* @param string
* @return
* @throws Exception
*/
private String encode(String string) throws Exception {
byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) {
hex.append("0");
}
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
* 把一个字符串以md5的放肆加密之后返回...
* 因为url路径里面可能存在一些不可用的特殊字符,所以使用这种方式处理一下
* @param string
* @return
* @throws Exception
*/
private String encode(String string) throws Exception {
byte[] hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) {
hex.append("0");
}
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
}