Android Open Source - AndroidImageCache Disk Cache






From Project

Back to project page AndroidImageCache.

License

The source code is released under:

Przemys?aw Jakubczyk Polidea Sp. z o.o. Copyright (c) 2012 All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the follo...

If you think the Android project AndroidImageCache 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 pl.polidea.thridparty;
/* w w  w  .j  a  v  a  2  s.  com*/
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import com.jakewharton.DiskLruCache;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import pl.polidea.utils.Utils;

/**
 * @author Przemys?aw Jakubczyk <przemyslaw.jakubczyk@pl.polidea.pl>
 */
public class DiskCache {

    private static final int APP_VERSION = 1;
    private static final int VALUE_COUNT = 1;
    private final CompressFormat compressFormat;
    private final int compressQuality;
    private DiskLruCache mDiskCache;

    public DiskCache(final String path, final long size, final CompressFormat compressFormat, final int compressQuality) {
        this.compressFormat = compressFormat;
        this.compressQuality = compressQuality;
        try {
            // XXX: this is done in UI thread !
            mDiskCache = openDiskLruCache(new File(path), APP_VERSION, VALUE_COUNT, size);
        } catch (final IOException e) {
            throw new IllegalArgumentException("Creating disk cache failed", e);
        }
    }

    private DiskLruCache openDiskLruCache(final File directory, final int appVersion, final int valueCount,
                                          final long size) throws IOException {
        return DiskLruCache.open(directory, appVersion, valueCount, size);
    }

    private boolean writeBitmapToFile(final Bitmap bitmap, final DiskLruCache.Editor editor) throws IOException {
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(editor.newOutputStream(0), 1024);
            return bitmap.isRecycled() ? true : bitmap.compress(compressFormat, compressQuality, out);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    public void put(final String key, final Bitmap data) {
        if (data == null) {
            Utils.log("null ERROR on: image put on disk cache " + key);
            return;
        }
        DiskLruCache.Editor editor = null;
        try {
            editor = mDiskCache.edit(key);
            if (editor == null) {
                return;
            }

            if (writeBitmapToFile(data, editor)) {
                mDiskCache.flush();
                editor.commit();
                Utils.log("image put on disk cache " + key);
            } else {
                editor.abort();
                Utils.log("abort ERROR on: image put on disk cache " + key);
            }
        } catch (final IOException e) {
            Utils.log("IOException ERROR on: image put on disk cache " + key);
            try {
                if (editor != null) {
                    editor.abort();
                }
            } catch (final IOException ignored) {
                Utils.log("Editor abort error");
            }
        }

        Utils.log("Cache disk current size: " + mDiskCache.size());

    }

    public Bitmap getBitmap(final 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, 1024);
                try {
                    Utils.log("Loading bitmap from disk");
                    bitmap = BitmapFactory.decodeStream(buffIn);
                } catch (final OutOfMemoryError e) {
                    Utils.log(e);
                }
            }
        } catch (final IOException e) {
            Utils.log("Loading bitmap from disk error.");
        } finally {
            if (snapshot != null) {
                snapshot.close();
            }
        }

        Utils.log(bitmap == null ? "" : "image read from disk " + key);

        return bitmap;

    }

    public boolean containsKey(final String key) {

        boolean contained = false;
        DiskLruCache.Snapshot snapshot = null;
        try {
            snapshot = mDiskCache.get(key);
            contained = snapshot != null;
        } catch (final IOException e) {
            Utils.log("Reading disk cache error");
        } finally {
            if (snapshot != null) {
                snapshot.close();
            }
        }

        return contained;

    }

    public boolean remove(final String key) throws IOException {
        return mDiskCache.remove(key);
    }

    public void clearCache() {
        final File directory = getDirectory();
        final long maxSize = getMaxSize();
        try {
            mDiskCache.delete();
            final String message = "disk cache CLEARED";
            Utils.log(message);
        } catch (final IOException e) {
            final File[] listFiles = directory.listFiles();
            deleteFiles(listFiles);
            Utils.log("Clearing disk cache error.", e);
        }
        try {
            mDiskCache = openDiskLruCache(directory, APP_VERSION, VALUE_COUNT, maxSize);
        } catch (final IOException e) {
            Utils.log("Opening disk cache error", e);
        }
    }

    private void deleteFiles(final File[] listFiles) {
        for (final File file : listFiles) {
            if (file.isDirectory()) {
                deleteFiles(file.listFiles());
                Utils.log("File " + file.getAbsolutePath() + " deleted with success? " + file.delete());
            }
            Utils.log("File " + file.getAbsolutePath() + " deleted with success? " + file.delete());
        }
    }

    public long getSize() {
        return mDiskCache.size();
    }

    public long getMaxSize() {
        return mDiskCache.getMaxSize();
    }

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

    public CompressFormat getCompressFormat() {
        return compressFormat;
    }

    public int getCompressQuality() {
        return compressQuality;
    }

}




Java Source Code List

pl.polidea.imagecache.BitmapLRUCache.java
pl.polidea.imagecache.CacheConfig.java
pl.polidea.imagecache.ImageCacheFactory.java
pl.polidea.imagecache.ImageCache.java
pl.polidea.imagecache.MemoryCache.java
pl.polidea.imagecache.OnCacheResultListener.java
pl.polidea.imagecache.StaticCachedImageCacheFactory.java
pl.polidea.thridparty.DiskCache.java
pl.polidea.thridparty.LinkedBlockingDeque.java
pl.polidea.thridparty.LruCache.java
pl.polidea.utils.Dimensions.java
pl.polidea.utils.StackBlockingDeque.java
pl.polidea.utils.StackPoolExecutor.java
pl.polidea.utils.TempFile.java
pl.polidea.utils.Utils.java
pl.polidea.webimagesampleapp.AndroidImageCacheExample.java
pl.polidea.webimageview.BitmapDecodeException.java
pl.polidea.webimageview.Bitmaps.java
pl.polidea.webimageview.DefaultBitmapProcessor.java
pl.polidea.webimageview.ImageViewUpdater.java
pl.polidea.webimageview.WebImageListener.java
pl.polidea.webimageview.WebImageView.java
pl.polidea.webimageview.net.StaticCachedWebClientFactory.java
pl.polidea.webimageview.net.WebCallback.java
pl.polidea.webimageview.net.WebClientFactory.java
pl.polidea.webimageview.net.WebClient.java
pl.polidea.webimageview.net.WebInterfaceImpl.java
pl.polidea.webimageview.net.WebInterface.java
pl.polidea.webimageview.processor.AbstractBitmapProcessorCreationChain.java
pl.polidea.webimageview.processor.BitmapProcessor.java
pl.polidea.webimageview.processor.BothWidthAndHeightFixed.java
pl.polidea.webimageview.processor.BothWidthAndHeightNotFixed.java
pl.polidea.webimageview.processor.OnlyHeightFixed.java
pl.polidea.webimageview.processor.OnlyWidthFixed.java
pl.polidea.webimageview.processor.ProcessorFactory.java
pl.polidea.webimageview.processor.Processor.java
pl.polidea.webimageview.processor.ProgramaticallyCreated.java
pl.polidea.webimageview.processor.Unknown.java