/** * 添加 数据到sdcard缓存中 * * @param url url地址 * @param data 数据信息 */ public void addToDiskCache(String url, byte[] data) { if (mDiskCache == null || url == null || data == null) { return; } // Add to disk cache byte[] key = Utils.makeKey(url); long cacheKey = Utils.crc64Long(key); ByteBuffer buffer = ByteBuffer.allocate(key.length + data.length); buffer.put(key); buffer.put(data); synchronized (mDiskCache) { try { mDiskCache.insert(cacheKey, buffer.array()); } catch (IOException ex) { // ignore. } } }
/** * 从sdcard中获取内存缓存 * * @param url 图片url地址 * @param buffer 填充缓存区 * @return 是否获得图片 */ public boolean getImageData(String url, BytesBuffer buffer) { if (mDiskCache == null) return false; byte[] key = Utils.makeKey(url); long cacheKey = Utils.crc64Long(key); try { LookupRequest request = new LookupRequest(); request.key = cacheKey; request.buffer = buffer.data; synchronized (mDiskCache) { if (!mDiskCache.lookup(request)) return false; } if (Utils.isSameKey(key, request.buffer)) { buffer.data = request.buffer; buffer.offset = key.length; buffer.length = request.length - buffer.offset; return true; } } catch (IOException ex) { // ignore. } return false; }
/** * Closes the disk cache associated with this ImageCache object. Note that this includes disk * access so this should not be executed on the main/UI thread. */ public void close() { if (mDiskCache != null) mDiskCache.close(); }
public void clearDiskCache() { if (mDiskCache != null) mDiskCache.delete(); }