package com.azy.xiaolong.news.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v4.util.LruCache;
public class BitmapLoad {
private static LruCache<String, Bitmap> cache;
private static File cachePath;
private BitmapCallback callback;
public BitmapLoad(Context context) {
int size=(int) (Runtime.getRuntime().maxMemory()/8);
if(cache==null){
cache=new LruCache<String, Bitmap>(size){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes()*value.getHeight();
}
};
}
if(cachePath==null){
cachePath=context.getExternalCacheDir();
if(!cachePath.exists()){
cachePath.mkdirs();
}
}
}
@SuppressWarnings("unused")
public Bitmap LoadBitmap(String url, BitmapCallback callback) {
this.callback = callback;
Bitmap bitmap = null;
bitmap = getBitmapToCache(url);
if (bitmap != null) {
return bitmap;
}
bitmap = getBitmapToSdcard(url);
if (bitmap != null) {
return bitmap;
}
getBitmapNet(url, callback);
return null;
}
private Bitmap getBitmapToCache(String url) {
return cache.get(url);
}
private void putBitmapToCache(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
private Bitmap getBitmapToSdcard(String url) {
String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
File file = new File(cachePath, fileName);
if (file.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
putBitmapToCache(url, bitmap);
return bitmap;
}
return null;
}
private void putBitmapToSdcard(String url, Bitmap bitmap) {
String fileName = url.substring(url.lastIndexOf('/') + 1, url.length());
File file = new File(cachePath, fileName);
OutputStream os;
try {
os = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public interface BitmapCallback {
public void BitmapOk(String str, Bitmap bitmap);
public void BitmapError(String url);
}
@SuppressLint("NewApi")
private void getBitmapNet(String url, BitmapCallback callback) {
BitmapAsyn2 asyn = new BitmapAsyn2();
if (Build.VERSION.SDK_INT >= 11) {
asyn.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
} else {
asyn.execute(url);
}
}
class BitmapAsyn2 extends AsyncTask<String, Void, Bitmap> {
String url;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {
this.url = params[0];
InputStream is = HttpClientUtil
.httpGet2InputStream(params[0], null);
if (is != null) {
Bitmap bitmap = BitmapFactory.decodeStream(is);
if (bitmap != null) {
putBitmapToCache(url, bitmap);
putBitmapToSdcard(url, bitmap);
try {
is.close();
return bitmap;
} catch (IOException e) {
}
}
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result != null) {
callback.BitmapOk(url, result);
} else {
callback.BitmapError(url);
}
}
}
}