Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DiskLruImageCache.java #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.LruCache;

import android.content.Context;
import android.graphics.Bitmap;
Expand All @@ -22,19 +23,21 @@
* Implementation of DiskLruCache by Jake Wharton
* modified from http://stackoverflow.com/questions/10185898/using-disklrucache-in-android-4-0-does-not-provide-for-opencache-method
*/
public class DiskLruImageCache implements ImageCache {
public class DiskLruImageCache extends LruCache<String, Bitmap> implements ImageCache {

private DiskLruCache mDiskCache;
private CompressFormat mCompressFormat = CompressFormat.JPEG;
private static int IO_BUFFER_SIZE = 8*1024;
private int mCompressQuality = 70;
private static final int APP_VERSION = 1;
private static final int VALUE_COUNT = 1;
private Context mContext;

public DiskLruImageCache(Context context,String uniqueName, int diskCacheSize,
CompressFormat compressFormat, int quality ) {
try {
final File diskCacheDir = getDiskCacheDir(context, uniqueName );
mContext = context;
mDiskCache = DiskLruCache.open( diskCacheDir, APP_VERSION, VALUE_COUNT, diskCacheSize );
mCompressFormat = compressFormat;
mCompressQuality = quality;
Expand Down Expand Up @@ -64,7 +67,7 @@ private File getDiskCacheDir(Context context, String uniqueName) {

@Override
public void putBitmap( String key, Bitmap data ) {

put(key, data); // Save on RAM
DiskLruCache.Editor editor = null;
try {
editor = mDiskCache.edit( key );
Expand Down Expand Up @@ -100,35 +103,40 @@ public void putBitmap( String key, Bitmap data ) {

@Override
public Bitmap getBitmap( String key ) {

Bitmap bitmap = null;
DiskLruCache.Snapshot snapshot = null;
try {

snapshot = mDiskCache.get( key );
if ( snapshot == null ) {
return null;
if(mContext == null)
return null;
if(isInternetAvailable(mContext))
return get(key); // Access RAM when net is available
else {
// Otherwise, access the DISK
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();
}
}
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();

if ( BuildConfig.DEBUG ) {
Log.d( "cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
}

return bitmap;
}

if ( BuildConfig.DEBUG ) {
Log.d( "cache_test_DISK_", bitmap == null ? "" : "image read from disk " + key);
}

return bitmap;

}

public boolean containsKey( String key ) {
Expand Down Expand Up @@ -165,7 +173,10 @@ public File getCacheFolder() {
return mDiskCache.getDirectory();
}


public boolean isInternetAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null ? cm.getActiveNetworkInfo().isConnectedOrConnecting() : false;
}



Expand Down