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 8470895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:38:44+00:00 2026-06-10T16:38:44+00:00

I need help understanding androids LruCache. I want to use to load images into

  • 0

I need help understanding androids LruCache. I want to use to load images into my gridview in order make the loading/scrolling better. Can someone post an example code using LruCache please. Thanks in advance.

  • 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-10T16:38:46+00:00Added an answer on June 10, 2026 at 4:38 pm

    Below is a class I made for using LruCache, this is based on the presentation Doing More With Less: Being a Good Android Citizen given at Google I/O 2012.

    Check out the movie for more information about what I’m doing in the TCImageLoader class:

    public class TCImageLoader implements ComponentCallbacks2 {
        private TCLruCache cache;
    
        public TCImageLoader(Context context) {
            ActivityManager am = (ActivityManager) context.getSystemService(
                Context.ACTIVITY_SERVICE);
            int maxKb = am.getMemoryClass() * 1024;
            int limitKb = maxKb / 8; // 1/8th of total ram
            cache = new TCLruCache(limitKb);
        }
    
        public void display(String url, ImageView imageview, int defaultresource) {
            imageview.setImageResource(defaultresource);
            Bitmap image = cache.get(url);
            if (image != null) {
                imageview.setImageBitmap(image);
            }
            else {
                new SetImageTask(imageview).execute(url);
            }
        }
    
        private class TCLruCache extends LruCache<String, Bitmap> {
    
            public TCLruCache(int maxSize) {
                super(maxSize);
            }
    
            @Override
            protected int sizeOf(ImagePoolKey key, Bitmap value) {
                int kbOfBitmap = value.getByteCount() / 1024;
                return kbOfBitmap;
            }
        }
    
        private class SetImageTask extends AsyncTask<String, Void, Integer> {
            private ImageView imageview;
            private Bitmap bmp;
    
            public SetImageTask(ImageView imageview) {
                this.imageview = imageview;
            }
    
            @Override
            protected Integer doInBackground(String... params) {
                String url = params[0];
                try {
                    bmp = getBitmapFromURL(url);
                    if (bmp != null) {
                        cache.put(url, bmp);
                    }
                    else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return 0;
                }
                return 1;
            }
    
            @Override
            protected void onPostExecute(Integer result) {
                if (result == 1) {
                    imageview.setImageBitmap(bmp);
                }
                super.onPostExecute(result);
            }
    
            private Bitmap getBitmapFromURL(String src) {
                try {
                    URL url = new URL(src);
                    HttpURLConnection connection
                        = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    Bitmap myBitmap = BitmapFactory.decodeStream(input);
                    return myBitmap;
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
    
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
        }
    
        @Override
        public void onLowMemory() {
        }
    
        @Override
        public void onTrimMemory(int level) {
            if (level >= TRIM_MEMORY_MODERATE) {
                cache.evictAll();
            }
            else if (level >= TRIM_MEMORY_BACKGROUND) {
                cache.trimToSize(cache.size() / 2);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help in understanding when and why to use Remote Service instead
I need help understanding if the way I'm trying to use a Ref Cursor
I'm trying to learn how to use PDO and I need help understanding something.
I need help understanding whats going on in the function, especially the return statements.
I need help understanding how to manage the API for an iPhone application that
I need help understanding how to demonize a process in Go. package main import
Kind of need help understanding what this code actually outputs. Does it out put
I'm fixing bugs for some application, and I need help understanding the following lines
I need some help understanding this bit of code pre { white-space: pre; white-space:
OK, I need some help understanding the process behind Facebook's website integration process and

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.