当前位置: 代码迷 >> Android >> 为何LruCache存不了值?
  详细解决方案

为何LruCache存不了值?

热度:102   发布时间:2016-04-27 22:58:42.0
为什么LruCache存不了值??????????????????
本帖最后由 j68205150 于 2015-04-17 19:41:49 编辑


class ImageCache {
private static LruCache<String, Drawable> map = null;

static{
long t = Runtime.getRuntime().maxMemory();
System.out.println("总的的缓存byte: " + t);
System.out.println("分配的缓存byte: " + (t / 4));
if(map != null){
map.evictAll();
}
map = new LruCache<String, Drawable>((int) t / 4){

@Override
protected void entryRemoved(boolean evicted, String key,
Drawable oldValue, Drawable newValue) {
System.out.println("===============================> entryRemoved: " + key);
super.entryRemoved(evicted, key, oldValue, newValue);
}

@Override
protected int sizeOf(String key, Drawable value) {
Bitmap bit = ((BitmapDrawable)value).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bit.compress(CompressFormat.JPEG, 75, bos);
int i = bos.toByteArray().length;
System.out.println(i + " <=========================== LruCache       key: " + key);
return i;
}
};
}

public static void put(String url, Drawable draw) {
map.put(url, draw);
}

public static Drawable getDrawable(Context context, String url) {
Drawable draw = null;
if(url != null && !"".equalsIgnoreCase(url)){
draw = getCache(url);
}

return draw;
}

private static Drawable getCache(String url){
if (map.get(url) != null) {
return map.get(url);
}
return null;
}

public static void remove(String url) {
map.remove(url);
}

}



class ImageLoadImpl implements IImageLoad {

private Context context = null;
private String url = null;
private int position = -1;
private IAsyncImageCallback iCallback = null;
private boolean isLocal = false;

private final Handler handler = new Handler() {

@Override
public void handleMessage(Message msg) {
iCallback.onFinish(msg.arg1, (Drawable) msg.obj);
}
};

public ImageLoadImpl(Context context, String url, int position,
ImageView imgView, IAsyncImageCallback cb) {
this.context = context;
this.url = url;
this.position = position;
this.iCallback = cb;
}

@Override
public void load() {
new LoadThread(url).start();
}

protected static Drawable loadLocalImage(Context context, String path) {  
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opt);

int w = opt.outWidth;
int h = opt.outHeight;
int r = 0;
try {
ExifInterface ei = new ExifInterface(path);
switch (i) {
case ExifInterface.ORIENTATION_ROTATE_90:
r = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
r = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
r = 270;
break;
}
} catch (IOException e1) {
e1.printStackTrace();
}

CompressFormat cf = getFormat(opt.outMimeType);

int max = Math.max(w, h);
float scale = 1;

if (max >= 320) {
scale = max / 320;
}

opt.inJustDecodeBounds = false;
opt.inSampleSize = (int) scale;
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inInputShareable = true;
opt.inPurgeable = true;

Drawable d = null;

if (cf != null) {
Bitmap map = BitmapFactory.decodeFile(path, opt);

if (r != 0) {
Matrix matrix = new Matrix();
matrix.setRotate(r);
matrix.postScale(1, 1);

// 旋转后,长和宽要对调
int t = w;
w = h;
h = t;

map = Bitmap.createBitmap(map, 0, 0, map.getWidth(),
map.getHeight(), matrix, true);
}
if(map != null){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
map.compress(cf, 75, bos);
try {
// 解决部分机型会缩小图片的问题
d = BitmapDrawable.createFromResourceStream(
context.getResources(), null,
new ByteArrayInputStream(bos.toByteArray()), null);
bos.reset();
bos.close();
} catch (IOException e) {
}
map.recycle();
map = null;
}else{
new File(path).delete();
}
}

return d;
}

@Override
protected void finalize() throws Throwable {
super.finalize();
ImageCache.remove(url);
}

class LoadThread extends Thread {
private String url = null;

public LoadThread(String url) {
this.url = url;
}

@Override
public void run() {
try {
Drawable draw = loadLocalImage(context, url);

if (draw != null) {
ImageCache.put(url, draw);
}

Message msg = new Message();
msg.arg1 = position;
msg.obj = draw;
handler.sendMessage(msg);
} catch (Exception e) {
//e.printStackTrace();
ImageCache.remove(url);
}
}
}

private static CompressFormat getFormat(String name) {
CompressFormat cf = null;
if(name != null){
if (name.endsWith("png") || name.endsWith("PNG")) {
cf = CompressFormat.PNG;
}

if (name.endsWith("jpeg") || name.endsWith("JPEG")
|| name.endsWith("jpg") || name.endsWith("JPG")) {
cf = CompressFormat.JPEG;
}
}

return cf;
}
}




请问为什么ImageCache里的LruCache存不了drawable????
只要ImageCache.put两三次,就调entryRemoved了,现在每次滚动就都要去重新new线程...

本地图片就十几张,平均每张十几kb...
页面一次显示8张图...

麻烦大家帮忙看看是什么问题...上面是代码示例


------解决思路----------------------
引用:
解决了...
finalize里remove去掉就好了...测试代码忘记删了..
唉...手贱惹的祸....
  相关解决方案