三级缓存:
首先查看内存缓存有没有,如果有则加载,没有则继续判断本地有没有,如果有就加载,没有就去网络请求;
private LruCache lruCache;
int size = (int) (Runtime.getRuntime().maxMemory() / 8); lruCache = new LruCache<String, Bitmap>(size) { @Override protected int sizeOf(String key, Bitmap value) { return value.getByteCount(); } };
if (lruCache.get(key) != null) { holder.iv_listview.setImageBitmap((Bitmap) lruCache.get(key)); } else { final File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), key.substring(0, key.lastIndexOf("/") + 1)); if (file.exists()) { Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); holder.iv_listview.setImageBitmap(bitmap); } else { new BitMapAsyncTask(context, new BitMapAsyncTask.CallBack() { @Override public void senBit(Bitmap bitmap, String url) { holder.iv_listview.setImageBitmap(bitmap); lruCache.put(url, bitmap); try { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } }, new BitMapAsyncTask.ErrorBack() { @Override public void sendError(String result) { } }).execute(key, ""); } }