/** 从缓存中获取图片 **/
public Bitmap getImage(final String url) {
final String path = getDirectory() + "/" + convertUrlToFileName(url);
System.out.println("path:"+path);
File file = new File(path);
if (file.exists()) {
try {
Bitmap bmp = BitmapFactory.decodeFile(path);
if (bmp == null) {
file.delete();
} else {
updateFileTime(path);
return bmp;
}
} catch (OutOfMemoryError err) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bmp = BitmapFactory.decodeFile(path, opts);
if (bmp == null) {
file.delete();
} else {
updateFileTime(path);
return bmp;
}
}
}
return null;
}
如果内存溢出,就对图片进行压缩,
opts.inSampleSize = 4;为压缩4倍