public void addBitmapToCache(String url, Bitmap bitmap) { if (url != null && bitmap != null) { if (mMemoryCache != null && mMemoryCache.get(url) == null) { mMemoryCache.put(url, bitmap); } if (mDiskCache == null) { return; } String str = hashKeyForDisk(url); try { if (mDiskCache.get(str) != null) return; DiskLruCache.Editor editor = mDiskCache.edit(str); if (editor == null) return; OutputStream output = editor.newOutputStream(0); bitmap.compress(mCacheParams.compressFormat, mCacheParams.compressQuality, output); editor.commit(); } catch (IOException e) { LogUtils.LOGE(TAG, "addBitmapToCache - " + e); } } }
public void put(String key, String value) { DiskLruCache.Editor edit = null; BufferedWriter bw = null; try { edit = editor(key); if (edit == null) return; OutputStream os = edit.newOutputStream(0); bw = new BufferedWriter(new OutputStreamWriter(os)); bw.write(value); edit.commit(); // write CLEAN } catch (IOException e) { e.printStackTrace(); try { // s edit.abort(); // write REMOVE } catch (IOException e1) { e1.printStackTrace(); } } finally { try { if (bw != null) bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
// ======================================= // ============== 序列化 数据 读写 ============= // ======================================= public void put(String key, Serializable value) { DiskLruCache.Editor editor = editor(key); ObjectOutputStream oos = null; if (editor == null) return; try { OutputStream os = editor.newOutputStream(0); oos = new ObjectOutputStream(os); oos.writeObject(value); oos.flush(); editor.commit(); } catch (IOException e) { e.printStackTrace(); try { editor.abort(); } catch (IOException e1) { e1.printStackTrace(); } } finally { try { if (oos != null) oos.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * 保存 byte数据 到 缓存中 * * @param key 保存的key * @param value 保存的数据 */ public void put(String key, byte[] value) { OutputStream out = null; DiskLruCache.Editor editor = null; try { editor = editor(key); if (editor == null) { return; } out = editor.newOutputStream(0); out.write(value); out.flush(); editor.commit(); // write CLEAN } catch (Exception e) { e.printStackTrace(); try { editor.abort(); // write REMOVE } catch (IOException e1) { e1.printStackTrace(); } } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * Adds a bitmap to both memory and disk cache. * * @param data Unique identifier for the bitmap to store * @param value The bitmap drawable to store */ public void addBitmapToCache(String data, BitmapDrawable value) { // BEGIN_INCLUDE(add_bitmap_to_cache) if (data == null || value == null) { return; } // Add to memory cache if (mMemoryCache != null) { if (RecyclingBitmapDrawable.class.isInstance(value)) { // The removed entry is a recycling drawable, so notify it // that it has been added into the memory cache ((RecyclingBitmapDrawable) value).setIsCached(true); } mMemoryCache.put(data, value); } synchronized (mDiskCacheLock) { // Add to disk cache if (mDiskLruCache != null) { final String key = hashKeyForDisk(data); OutputStream out = null; try { DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot == null) { final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); value .getBitmap() .compress(mCacheParams.compressFormat, mCacheParams.compressQuality, out); editor.commit(); out.close(); } } else { snapshot.getInputStream(DISK_CACHE_INDEX).close(); } } catch (final IOException e) { Log.e(TAG, "addBitmapToCache - " + e); } catch (Exception e) { Log.e(TAG, "addBitmapToCache - " + e); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { } } } } // END_INCLUDE(add_bitmap_to_cache) }
/** * 把数据写入缓存 * * @param name 表名 * @param content JSON数据串 */ public void write(String name, String content) { try { DiskLruCache.Editor editors = diskLruCache.edit(name); if (editors != null) { OutputStream out = editors.newOutputStream(0); byte[] bytes = content.getBytes(); out.write(bytes); out.flush(); editors.commit(); diskLruCache.flush(); } } catch (IOException e) { e.printStackTrace(); } }
/** * Writes a Bitmap to the disk cache. * * @param request the write request to be served. */ private static void writeBitmapToDiskCache(WriteRequest request) { try { String key = hashKeyForDisk(request.mUrl); // This shouldn't happen either, but again, better safe than sorry if (mDiskCache != null) { DiskLruCache.Editor editor = mDiskCache.edit(key); if (editor != null) { OutputStream outputStream = editor.newOutputStream(DISK_CACHE_INDEX); request.mBitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); editor.commit(); } } else { Log.d("DiskCache", "Cache is closed!"); } } catch (IOException iox) { iox.printStackTrace(); } }
public void put(final Uri uri, final InputStream inputStream) { checkNotOnMainThread(); // First we need to save the stream contents to a temporary file, so it // can be read multiple times File tmpFile = null; try { tmpFile = File.createTempFile("bitmapcache_", null, mTempDir); // Pipe InputStream to file IoUtils.copy(inputStream, tmpFile); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error writing to saving stream to temp file: " + uri.toString(), e); } if (null != tmpFile) { // Try and decode File FileInputStreamProvider provider = new FileInputStreamProvider(tmpFile); if (provider != null) { if (null != mDiskCache) { final String key = transformUrlForDiskCacheKey(uri); final ReentrantLock lock = getLockForDiskCacheEdit(uri); lock.lock(); try { DiskLruCache.Editor editor = mDiskCache.edit(key); IoUtils.copy(tmpFile, editor.newOutputStream(0)); editor.commit(); } catch (IOException e) { Log.e(Constants.LOG_TAG, "Error writing to disk cache. URL: " + uri, e); } finally { lock.unlock(); scheduleDiskCacheFlush(); } } // Finally, delete the temporary file tmpFile.delete(); } } }
/** * Adds a new image to the memory and disk caches * * @param data The key used to store the image * @param bitmap The {@link Bitmap} to cache */ public void addBitmapToCache(final String data, final Bitmap bitmap) { if (data == null || bitmap == null) { return; } // Add to memory cache addBitmapToMemCache(data, bitmap); // Add to disk cache if (mDiskCache != null) { final String key = hashKeyForDisk(data); OutputStream out = null; try { final DiskLruCache.Snapshot snapshot = mDiskCache.get(key); if (snapshot == null) { final DiskLruCache.Editor editor = mDiskCache.edit(key); if (editor != null) { out = editor.newOutputStream(DISK_CACHE_INDEX); bitmap.compress(COMPRESS_FORMAT, COMPRESS_QUALITY, out); editor.commit(); out.close(); flush(); } } else { snapshot.getInputStream(DISK_CACHE_INDEX).close(); } } catch (final IOException e) { Log.e(TAG, "addBitmapToCache - " + e); } finally { try { if (out != null) { out.close(); out = null; } } catch (final IOException e) { Log.e(TAG, "addBitmapToCache - " + e); } catch (final IllegalStateException e) { Log.e(TAG, "addBitmapToCache - " + e); } } } }
/** * Adds a bitmap to both memory and disk cache * * @param data Unique identifier for the bitmap to store * @param bitmap The bitmap to store */ public void addBitmapToCache(String data, Bitmap bitmap) { if (data == null || bitmap == null) { return; } // Add the bitmap to memory cache if (mMemoryCache != null && mMemoryCache.get(data) == null) { mMemoryCache.put(data, bitmap); } // Add the bitmap to disk cache synchronized (mDiskCacheLock) { final String key = hashKeyForDisk(data); OutputStream outputStream = null; try { DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key); if (snapshot == null) { final DiskLruCache.Editor editor = mDiskLruCache.edit(key); if (editor != null) { outputStream = editor.newOutputStream(DISK_CACHE_INDEX); bitmap.compress( mCacheParams.compressFormat, mCacheParams.compressQuality, outputStream); editor.commit(); outputStream.close(); } } else { snapshot.getInputStream(DISK_CACHE_INDEX).close(); ; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
@Override public boolean save(String imageUri, Bitmap bitmap) throws IOException { DiskLruCache.Editor editor = cache.edit(getKey(imageUri)); if (editor == null) { return false; } OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize); boolean savedSuccessfully = false; try { savedSuccessfully = bitmap.compress(compressFormat, compressQuality, os); } finally { IoUtils.closeSilently(os); } if (savedSuccessfully) { editor.commit(); } else { editor.abort(); } return savedSuccessfully; }
@Override public boolean save(String imageUri, InputStream imageStream, IoUtils.CopyListener listener) throws IOException { DiskLruCache.Editor editor = cache.edit(getKey(imageUri)); if (editor == null) { return false; } OutputStream os = new BufferedOutputStream(editor.newOutputStream(0), bufferSize); boolean copied = false; try { copied = IoUtils.copyStream(imageStream, os, listener, bufferSize); } finally { IoUtils.closeSilently(os); if (copied) { editor.commit(); } else { editor.abort(); } } return copied; }
/** * The main process method, which will be called by the ImageWorker in the AsyncTask background * thread. * * @param data The data to load the bitmap, in this case, a regular http URL * @return The downloaded and resized bitmap */ private Bitmap processBitmap(String data) { if (CacheConfig.DEBUG) { Log.d(TAG, "processBitmap - " + data); } final String key = ImageCache.hashKeyForDisk(data); FileDescriptor fileDescriptor = null; FileInputStream fileInputStream = null; DiskLruCache.Snapshot snapshot; synchronized (mHttpDiskCacheLock) { // Wait for disk cache to initialize while (mHttpDiskCacheStarting) { try { mHttpDiskCacheLock.wait(); } catch (InterruptedException e) { } } if (mHttpDiskCache != null) { try { snapshot = mHttpDiskCache.get(key); if (snapshot == null) { if (CacheConfig.DEBUG) { Log.d(TAG, "processBitmap, not found in http cache, downloading..."); } DiskLruCache.Editor editor = mHttpDiskCache.edit(key); if (editor != null) { if (downloadUrlToStream(data, editor.newOutputStream(DISK_CACHE_INDEX))) { editor.commit(); } else { editor.abort(); } } snapshot = mHttpDiskCache.get(key); } if (snapshot != null) { fileInputStream = (FileInputStream) snapshot.getInputStream(DISK_CACHE_INDEX); fileDescriptor = fileInputStream.getFD(); } } catch (IOException e) { Log.e(TAG, "processBitmap - " + e); } catch (IllegalStateException e) { Log.e(TAG, "processBitmap - " + e); } finally { if (fileDescriptor == null && fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { } } } } } Bitmap bitmap = null; if (fileDescriptor != null) { bitmap = decodeSampledBitmapFromDescriptor( fileDescriptor, mImageWidth, mImageHeight, getImageCache()); } if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { } } return bitmap; }