本文整理汇总了Java中com.jakewharton.DiskLruCache类的典型用法代码示例。如果您正苦于以下问题:Java DiskLruCache类的具体用法?Java DiskLruCache怎么用?Java DiskLruCache使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DiskLruCache类属于com.jakewharton包,在下文中一共展示了DiskLruCache类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeToDiskCache
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
protected boolean writeToDiskCache(URL url, Bitmap bitmap){
boolean isOk = false;
if(diskCache != null){
String cacheKey = MD5.encode(url.toString());
try {
DiskLruCache.Editor editor = diskCache.edit(cacheKey);
if(editor != null){
OutputStream out = editor.newOutputStream(0);
bitmap.compress(CompressFormat.PNG, 95, out);
out.close();
editor.commit();
//Log.d(Constants.TAG_EMOP, "write to disk key:" + cacheKey + ", url:" + url.toString());
isOk = true;
editor = null;
}
} catch (IOException e) {
isOk = false;
Log.d(Constants.TAG_EMOP, "write disk lru cache error:" + e.toString(), e);
}
}else {
Log.w(Constants.TAG_EMOP, "read disk lru cache is null");
}
return isOk;
}
示例2: initHttpDiskCache
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
private void initHttpDiskCache() {
if (!mHttpCacheDir.exists()) {
mHttpCacheDir.mkdirs();
}
synchronized (mHttpDiskCacheLock) {
long usableSpace = ImageCache.getUsableSpace(mHttpCacheDir);
try {
if (usableSpace > HTTP_CACHE_SIZE) {
usableSpace = HTTP_CACHE_SIZE;
}
mHttpDiskCache = DiskLruCache.open(mHttpCacheDir, 1, 1, usableSpace);
if (BuildConfig.DEBUG) {
Log.d(TAG, "HTTP cache initialized");
}
} catch (IOException e) {
mHttpDiskCache = null;
}
mHttpDiskCacheStarting = false;
mHttpDiskCacheLock.notifyAll();
}
}
示例3: readFromDiskCache
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
protected Bitmap readFromDiskCache(URL url){
Bitmap bm = null;
if(diskCache != null){
//diskCache.flush()
String cacheKey = MD5.encode(url.toString());
try {
DiskLruCache.Snapshot snapshot = diskCache.get(cacheKey);
if(snapshot != null){
InputStream in = snapshot.getInputStream(0);
bm = BitmapFactory.decodeStream(in);
in.close();
snapshot.close();
snapshot = null;
//Log.d(Constants.TAG_EMOP, "read from disk key:" + cacheKey + ", url:" + url.toString());
}
} catch (IOException e) {
Log.d(Constants.TAG_EMOP, "read disk lru cache error:" + e.toString(), e);
}
}else {
Log.w(Constants.TAG_EMOP, "read disk lru cache is null");
}
return bm;
}
示例4: putBitmap
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
@Override
public void putBitmap(String key, Bitmap data) {
DiskLruCache.Editor editor = null;
try {
editor = mDiskCache.edit(key);
if (editor == null) {
return;
}
if (writeBitmapToFile(data, editor)) {
mDiskCache.flush();
editor.commit();
} else {
editor.abort();
}
} catch (IOException e) {
try {
if (editor != null) {
editor.abort();
}
} catch (IOException ignored) {
}
}
}
示例5: containsKey
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
public boolean containsKey(String key) {
boolean contained = false;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get(key);
contained = snapshot != null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (snapshot != null) {
snapshot.close();
}
}
return contained;
}
示例6: writeBitmapToFile
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
private boolean writeBitmapToFile( Bitmap bitmap, DiskLruCache.Editor editor ) throws IOException, FileNotFoundException {
OutputStream out = null;
try {
out = new BufferedOutputStream( editor.newOutputStream(0), IO_BUFFER_SIZE );
return bitmap.compress(BITMAP_COMPRESS_FORMAT, BITMAP_COMPRESS_QUALITY, out);
} finally {
if ( out != null ) {
out.close();
}
}
}
示例7: getImage
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
public BufferedInputStream getImage(String key) throws IOException{
DiskLruCache.Snapshot snapshot;
if(imageCache == null)
return null;
snapshot = imageCache.get(Integer.toString(key.hashCode()));
if (snapshot == null)
return null;
return new BufferedInputStream(snapshot.getInputStream(0));
}
示例8: ImageCache
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
public ImageCache(File root, final int cacheSize){
this.cacheRoot = root;
memCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
int size = bitmap.getHeight() * bitmap.getWidth() * 4;
return size;
}
};
lockCache = new LruCache<String, Lock>(200) {
@Override
protected int sizeOf(String key, Lock obj) {
return 1;
}
};
if(diskCache == null && root != null){
try {
Log.d(Constants.TAG_EMOP, "create LRU cache in:" + root.getAbsolutePath());
diskCache = DiskLruCache.open(root, 1, 1, 1024 * 1024 * 64);
} catch (IOException e) {
Log.w(Constants.TAG_EMOP, "open disk cache error:" + e.toString(), e);
}
}
}
示例9: cleanUpDiskCache
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
public void cleanUpDiskCache(){
if(diskCache != null){
File f = diskCache.getDirectory();
try {
memCache.evictAll();
diskCache.delete();
diskCache = DiskLruCache.open(f, 1, 1, 1024 * 1024 * 64);
} catch (IOException e) {
Log.w(Constants.TAG_EMOP, "open disk cache error:" + e.toString(), e);
}
}
}
示例10: initDiskCache
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
/**
* Initializes the disk cache. Note that this includes disk access so this should not be
* executed on the main/UI thread. By default an ImageCache does not initialize the disk
* cache when it is created, instead you should call initDiskCache() to initialize it on a
* background thread.
*/
public void initDiskCache() {
// Set up disk cache
synchronized (mDiskCacheLock) {
if (mDiskLruCache == null || mDiskLruCache.isClosed()) {
File diskCacheDir = mCacheParams.diskCacheDir;
if (mCacheParams.diskCacheEnabled && diskCacheDir != null) {
if (!diskCacheDir.exists()) {
diskCacheDir.mkdirs();
}
if (getUsableSpace(diskCacheDir) > mCacheParams.diskCacheSize) {
try {
mDiskLruCache = DiskLruCache.open(
diskCacheDir, 1, 1, mCacheParams.diskCacheSize);
if (BuildConfig.DEBUG) {
Log.d(TAG, "Disk cache initialized");
}
} catch (final IOException e) {
mCacheParams.diskCacheDir = null;
Log.e(TAG, "initDiskCache - " + e);
}
}
}
}
mDiskCacheStarting = false;
mDiskCacheLock.notifyAll();
}
}
示例11: DiskLruImageCache
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
public DiskLruImageCache(Context context, String uniqueName, int diskCacheSize,
Bitmap.CompressFormat compressFormat, int quality) {
try {
final File diskCacheDir = getDiskCacheDir(context, uniqueName);
mDiskCache = DiskLruCache.open(diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize);
mCompressFormat = compressFormat;
mCompressQuality = quality;
} catch (IOException e) {
e.printStackTrace();
}
}
示例12: writeBitmapToFile
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
private boolean writeBitmapToFile(Bitmap bitmap, DiskLruCache.Editor editor)
throws IOException, FileNotFoundException {
OutputStream out = null;
try {
out = new BufferedOutputStream(editor.newOutputStream(0), IO_BUFFER_SIZE);
return bitmap.compress(mCompressFormat, mCompressQuality, out);
} finally {
if (out != null) {
out.close();
}
}
}
示例13: getBitmap
import com.jakewharton.DiskLruCache; //导入依赖的package包/类
@Override
public Bitmap getBitmap(String key) {
Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
try {
snapshot = mDiskCache.get(key);
if (snapshot == null) {
return null;
}
final InputStream in = snapshot.getInputStream(0);
if (in != null) {
final BufferedInputStream buffIn =
new BufferedInputStream(in, IO_BUFFER_SIZE);
bitmap = BitmapFactory.decodeStream(buffIn);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (snapshot != null) {
snapshot.close();
}
}
return bitmap;
}