标签:exists pack out string connect private nts this cte
public class ImageHelper {
private final LruCache<String, Bitmap> mCache;
private Context context;
public ImageHelper(Context context) {
this.context = context;
//创建对象的时候就去分配一个内存
//使用LruCache对象进行内存的缓存...底层是LinedHashMap..特点:当内存不够用的时候,可以自动移除最近使用次数最少的对象
//int maxSize表示内存缓存分配的最大值..maxMemory()默认给一个应用分配的最大值是16M,可以是他的1/8,,,也可以是可用内存的1/4
int maxSize = (int) (Runtime.getRuntime().freeMemory()/4);
mCache = new LruCache<String, Bitmap>(maxSize){
//计算每一张图片的大小
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() *value.getHeight();
}
};
}
/**
* 展示图片的方法
* @param imageView
* @param url
*/
public void displayImage(ImageView imageView, String url){
//1.获取内存中的图片...如果获取到,结束
Bitmap bitmap = mCache.get(url);
if (bitmap != null){
imageView.setImageBitmap(bitmap);
return;
}
//2.去sd卡里面获取图片,,,如果获取到加载内存,然后结束
bitmap = loadFromLocal(url);
if (bitmap != null){
imageView.setImageBitmap(bitmap);
return;
}
//3.去网络上获取图片,,,获取到之后添加到sd卡,添加到内存,,,设置图片显示
loadFromNet(imageView,url);
}
/**
* 从网络上获取图片
* @param imageView
* @param url
*/
private void loadFromNet(final ImageView imageView, final String url) {
AsyncTask<Void, Void, Bitmap> asyncTask = new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... voids) {
try {
URL path = new URL(url);
HttpURLConnection connection = (HttpURLConnection) path.openConnection();
//设置
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//返回
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
//写入sd卡
writeToSdCard(url,bitmap);
//添加到内存缓存
mCache.put(url,bitmap);
//显示
imageView.setImageBitmap(bitmap);
}
};
asyncTask.execute();
}
/**
* 将下载的图片写入sd卡
* @param url
* @param bitmap
*/
private void writeToSdCard(String url, Bitmap bitmap) {
try {
//使用一个编码的路径
String path = encode(url);
File file = new File(getSdCacheDir(), path);
//获取输出流
//open failed: EROFS (Read-only file system)...只读模式
FileOutputStream fileOutputStream = new FileOutputStream(file);
//把bitmap图片压缩的输出流中,然后存到文件,,,JPEG以图片的格式,,100表示压缩率为0,,,70表示压缩率是30%
bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream);
//关闭流
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从sk卡中获取图片
* @param url
* @return
*/
private Bitmap loadFromLocal(String url) {
try {
//处理一下图片的路径
String path = encode(url);
//权限
String storageState = Environment.getExternalStorageState();
if (storageState.equals(Environment.MEDIA_MOUNTED)){
File file = new File(getSdCacheDir(), path);
if (file.exists()){
//转换成bitmap
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
//存在...放到内存
mCache.put(url,bitmap);
//...返回
return bitmap;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* sd卡存放图片的目录
* @return
*/
private File getSdCacheDir() {
File dir;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//当前应用的包名
dir = new File(Environment.getExternalStorageDirectory(),"/Android/data/"+context.getPackageName()+"/pic");
if (! dir.exists()){
dir.mkdirs();
}
return dir;
}
return null;
}
/**
* 把一个字符串以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();
}
}
标签:exists pack out string connect private nts this cte
原文地址:http://www.cnblogs.com/cuijaijun1224/p/7476749.html