Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:28:24+00:00 2026-06-02T13:28:24+00:00

Have anyone worked on DiskLruCache? http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html provides an example of using this caching method.

  • 0

Have anyone worked on DiskLruCache?

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

provides an example of using this caching method. But i observe the implementation for DiskLruCache in 4.0 source code does not have defined methods. Any idea how to use this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:28:26+00:00Added an answer on June 2, 2026 at 1:28 pm

    You can use Picasso as image loader.

    Or

    This is what I did:

    I created a class named DiskLruImageCache with a DiskLruCache (the one from Jake Wharton) object and the same methods in the simple implementation on the dev guide (displaying bitmaps efficiently):

    public class DiskLruImageCache {
    
        private DiskLruCache mDiskCache;
        private CompressFormat mCompressFormat = CompressFormat.JPEG;
        private int mCompressQuality = 70;
        private static final int APP_VERSION = 1;
        private static final int VALUE_COUNT = 1;
        private static final String TAG = "DiskLruImageCache";
    
        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 ), Utils.IO_BUFFER_SIZE );
                return bitmap.compress( mCompressFormat, mCompressQuality, out );
            } finally {
                if ( out != null ) {
                    out.close();
                }
            }
        }
    
        private File getDiskCacheDir(Context context, String uniqueName) {
    
        // Check if media is mounted or storage is built-in, if so, try and use external cache dir
        // otherwise use internal cache dir
            final String cachePath =
                Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                        !Utils.isExternalStorageRemovable() ?
                        Utils.getExternalCacheDir(context).getPath() :
                        context.getCacheDir().getPath();
    
            return new File(cachePath + File.separator + uniqueName);
        }
    
        public void put( 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) {
                }           
            }
    
        }
    
        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, Utils.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();
        }
    
    }
    

    Utils source code is:

    public class Utils {
        public static final int IO_BUFFER_SIZE = 8 * 1024;
    
        private Utils() {};
    
        public static boolean isExternalStorageRemovable() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                return Environment.isExternalStorageRemovable();
            }
            return true;
        }
    
        public static File getExternalCacheDir(Context context) {
            if (hasExternalCacheDir()) {
                return context.getExternalCacheDir();
            }
    
            // Before Froyo we need to construct the external cache dir ourselves
            final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/";
            return new File(Environment.getExternalStorageDirectory().getPath() + cacheDir);
        }
    
        public static boolean hasExternalCacheDir() {
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
        }
    
    }
    

    Remember to put

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    on your AndroidManifest.xml

    I figured out this way by reading this code and the javadoc from Jake Wharton’s DiskLruCache

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have worked with cXML and C#? I'm working in a project that
anyone ever worked with the jQuery Validate Plugin. I have like the following function.
Have anyone successfully managed to setup a combined Java/C++ project for Eclipse? What I
Does anyone have a nice solution for GalleryView when I want to display photos
Does anyone have any idea how to change the thickness of the text pointer
Anyone have an example snippet showing how to scale a Font using AffineTransform? Thanks.
Does anyone have some code that will take a TimeZoneInfo field from .NET and
DOes anyone have any good information with regards to when to log, I was
Does anyone have any tips that could help speed up a process of breaking
Anyone have any experience yet getting Radiant CMS extensions to actually make it onto

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.