Android Open Source - Dreamer Disk Lru Image Cache






From Project

Back to project page Dreamer.

License

The source code is released under:

Apache License

If you think the Android project Dreamer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.android.fancyblurdemo.app.imageblur;
/*  www .j ava2 s.co  m*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.android.fancyblurdemo.app.BuildConfig;
import com.android.fancyblurdemo.volley.toolbox.ImageLoader;
import com.jakewharton.disklrucache.DiskLruCache;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Modified version of Jake Wharton's DiskLruCache
 * Created by kevin.marlow on 3/26/14.
 */
public class DiskLruImageCache implements ImageLoader.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;

    public DiskLruImageCache(Context context,String uniqueName, int diskCacheSize,
                             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();
        }
    }

    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();
            }
        }
    }

    private File getDiskCacheDir(Context context, String uniqueName) {

        final String cachePath = context.getCacheDir().getPath();
        return new File(cachePath + File.separator + uniqueName);
    }

    @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();
                if ( BuildConfig.DEBUG ) {
                    Log.d("cache_test_DISK_", "image put on disk cache " + key);
                }
            } else {
                editor.abort();
                if ( BuildConfig.DEBUG ) {
                    Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
                }
            }
        } catch (IOException e) {
            if ( BuildConfig.DEBUG ) {
                Log.d( "cache_test_DISK_", "ERROR on: image put on disk cache " + key );
            }
            try {
                if ( editor != null ) {
                    editor.abort();
                }
            } catch (IOException ignored) {
            }
        }

    }

    @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();
            }
        }

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

        return bitmap;

    }

    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;

    }

    public void clearCache() {
        if ( BuildConfig.DEBUG ) {
            Log.d( "cache_test_DISK_", "disk cache CLEARED");
        }
        try {
            mDiskCache.delete();
        } catch ( IOException e ) {
            e.printStackTrace();
        }
    }

    public File getCacheFolder() {
        return mDiskCache.getDirectory();
    }
}




Java Source Code List

com.android.fancyblurdemo.app.BitmapLruImageCache.java
com.android.fancyblurdemo.app.BlurImageView.java
com.android.fancyblurdemo.app.ContextWrapperEdgeEffect.java
com.android.fancyblurdemo.app.DreamAdapter.java
com.android.fancyblurdemo.app.EdgeEffectViewPager.java
com.android.fancyblurdemo.app.FlickrDream.java
com.android.fancyblurdemo.app.FlickrPhoto.java
com.android.fancyblurdemo.app.FlickrRequest.java
com.android.fancyblurdemo.app.FlickrScroller.java
com.android.fancyblurdemo.app.MainActivity.java
com.android.fancyblurdemo.app.MainApplication.java
com.android.fancyblurdemo.app.PhotoFragment.java
com.android.fancyblurdemo.app.PhotoSizeRequest.java
com.android.fancyblurdemo.app.PreCompositeTextTask.java
com.android.fancyblurdemo.app.RobotoTextView.java
com.android.fancyblurdemo.app.TopSwipeLayout.java
com.android.fancyblurdemo.app.VolleyManager.java
com.android.fancyblurdemo.app.imageblur.BlurDispatcher.java
com.android.fancyblurdemo.app.imageblur.BlurError.java
com.android.fancyblurdemo.app.imageblur.BlurManager.java
com.android.fancyblurdemo.app.imageblur.BlurQueue.java
com.android.fancyblurdemo.app.imageblur.BlurRequest.java
com.android.fancyblurdemo.app.imageblur.BlurResponseDelivery.java
com.android.fancyblurdemo.app.imageblur.BlurResponse.java
com.android.fancyblurdemo.app.imageblur.DiskLruImageCache.java
com.android.fancyblurdemo.app.imageblur.ImageBlurrer.java
com.android.fancyblurdemo.volley.AuthFailureError.java
com.android.fancyblurdemo.volley.CacheDispatcher.java
com.android.fancyblurdemo.volley.Cache.java
com.android.fancyblurdemo.volley.DefaultRetryPolicy.java
com.android.fancyblurdemo.volley.ExecutorDelivery.java
com.android.fancyblurdemo.volley.NetworkDispatcher.java
com.android.fancyblurdemo.volley.NetworkError.java
com.android.fancyblurdemo.volley.NetworkResponse.java
com.android.fancyblurdemo.volley.Network.java
com.android.fancyblurdemo.volley.NoConnectionError.java
com.android.fancyblurdemo.volley.ParseError.java
com.android.fancyblurdemo.volley.RequestQueue.java
com.android.fancyblurdemo.volley.Request.java
com.android.fancyblurdemo.volley.ResponseDelivery.java
com.android.fancyblurdemo.volley.Response.java
com.android.fancyblurdemo.volley.RetryPolicy.java
com.android.fancyblurdemo.volley.ServerError.java
com.android.fancyblurdemo.volley.TimeoutError.java
com.android.fancyblurdemo.volley.VolleyError.java
com.android.fancyblurdemo.volley.VolleyLog.java
com.android.fancyblurdemo.volley.toolbox.AndroidAuthenticator.java
com.android.fancyblurdemo.volley.toolbox.Authenticator.java
com.android.fancyblurdemo.volley.toolbox.BasicNetwork.java
com.android.fancyblurdemo.volley.toolbox.ByteArrayPool.java
com.android.fancyblurdemo.volley.toolbox.ClearCacheRequest.java
com.android.fancyblurdemo.volley.toolbox.DiskBasedCache.java
com.android.fancyblurdemo.volley.toolbox.HttpClientStack.java
com.android.fancyblurdemo.volley.toolbox.HttpHeaderParser.java
com.android.fancyblurdemo.volley.toolbox.HttpStack.java
com.android.fancyblurdemo.volley.toolbox.HurlStack.java
com.android.fancyblurdemo.volley.toolbox.ImageLoader.java
com.android.fancyblurdemo.volley.toolbox.ImageRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonArrayRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonObjectRequest.java
com.android.fancyblurdemo.volley.toolbox.JsonRequest.java
com.android.fancyblurdemo.volley.toolbox.NetworkImageView.java
com.android.fancyblurdemo.volley.toolbox.NoCache.java
com.android.fancyblurdemo.volley.toolbox.PoolingByteArrayOutputStream.java
com.android.fancyblurdemo.volley.toolbox.RequestFuture.java
com.android.fancyblurdemo.volley.toolbox.StringRequest.java
com.android.fancyblurdemo.volley.toolbox.Volley.java