Android: LruCache issue with Bitmap

Android: LruCache issue with Bitmap

By : Hemchul
Date : November 16 2020, 06:23 AM
To fix this issue Finally I figured out the problem. It is becuase of the adapter. in the adapter I have set some ImageView as invisible if no thumbnail is needed. when the user scrolls the list view, such ImageView instance will be reused, but the visibility is not updated.
so the cache itself is OK now. The solution is to check the visibility of the ImageView and update it if needed.
code :

Share : facebook icon twitter icon
When should I recycle a bitmap using LRUCache?

When should I recycle a bitmap using LRUCache?

By : Ernesto A Estrada
Date : March 29 2020, 07:55 AM
it helps some times
I believe that when the LRUCache evicts an image to make room for another one, the memory is not being freed.
Using LruCache to Store Bitmap in Memory

Using LruCache to Store Bitmap in Memory

By : Aaron Hans
Date : March 29 2020, 07:55 AM
seems to work fine You're on the right track, but I couldn't tell where in your code you account for view recycling in your List/GridView. You are indeed reusing the convertView passed to getView, but launching a new AsyncTask in every getView will lead to the tasks loading the images, but then inserting them in the wrong list rows if the view it was started by gets recycled. My advice would be to check out Volley, Google's new networking library. It has a NetworkImageView class that will load an image asynchronously into an ImageView. The reason I recommend it is caching, both on disk and in memory, and view recycling are handled for you.
Volley: https://developers.google.com/events/io/sessions/325304728
code :
// From com.android.volley.toolbox.ImageLoader
public interface ImageCache {
    public Bitmap getBitmap(String url);
    public void putBitmap(String url, Bitmap bitmap);

// My ImageCache implementation
public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {
    private static final int DEFAULT_CACHE_SIZE = (int) (Runtime.getRuntime().maxMemory() / 1024) / 8;

    public BitmapLruCache() {

    public BitmapLruCache(int maxSize) {

    public Bitmap getBitmap(String url) {
        return get(url);

    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);

    protected int sizeOf(String key, Bitmap value) {
        return value == null ? 0 : value.getRowBytes() * value.getHeight() / 1024;
ListView recycles Bitmap without noticing in lrucache

ListView recycles Bitmap without noticing in lrucache

By : user2677914
Date : March 29 2020, 07:55 AM
it fixes the issue
When I scroll down the first time, the new Image views appear with a previous bitmap for an instant before the task finishes setting up the correct bitmap (even though I draw a resource Bitmap on the adapter's getView) I don't understand why.
code :
if (!getItem(position).paintCover(lViewHolder.getImageView())) {
       Log.i(TAG,"getView: task");
       new GalleryItemTask(position, lViewHolder)
               .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
   //what should the adapter do if paintCover is true?
Implementing LruCache for caching bitmap images

Implementing LruCache for caching bitmap images

By : Barett Mims
Date : March 29 2020, 07:55 AM
wish helps you To calculate the in-memory size of the bitmap prior to API level 12 you may use this code:
code :
int bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
protected int sizeOf(String key, Bitmap bitmap) {
        return bitmap.getByteCount();
    } else {
        return bitmap.getRowBytes() * bitmap.getHeight();
Using LruCache: Is cache attatched to a LruCache instance?

Using LruCache: Is cache attatched to a LruCache instance?

By : glcafuni
Date : March 29 2020, 07:55 AM
I hope this helps . An LruCache object just stores references to objects in memory. As soon as you lose the reference to the LruCache, the LruCache object and all of the objects within that cache are garbage collected. There's nothing stored to disk.
Related Posts Related Posts :
  • Android Studio gradle build failed for hello world app
  • Slide a layout up from bottom of screen
  • Play HTML5 video in iOS/Android native media player
  • How to rescale a bitmap file in android?
  • No view found for id 0x for fragment after rotation?
  • Define different themable styles to the same widget types
  • ProGuard not working in my app
  • FragmentTransaction from BaseAdapter
  • Connecting multiple devices in a Wifi Direct group
  • Why TabActivity is deprecated in android
  • How to capture low resolution picture using android camera
  • android preferenceactivity padding in landscape orientation
  • How can I store cookie in webview?
  • Passing variables from one activity to another
  • Create Multicolored Pie Chart in Android Studio
  • how to kill progress dialog in android
  • Show Error on the tip of the Edit Text Android
  • How do I use getWindowManager() by another java file
  • fetching images from gallery on android phones with internal storage
  • How to discover devices like Google TV and Nexus-Q using MediaRoute API on Android?
  • "Unfortunately your app stopped working"- Facebook Integration for Android
  • How to animate button in android?
  • Resource Not Found Exception in class without context or activity
  • Launching Android Netflix App And Passing Video Id
  • Making a Phone Call with a Number Extension
  • NullPointerException at BoringLayout.isBoring is anything but boring
  • java.lang.RuntimeException: Unable to get provider
  • Testing custom Views with Robolectric
  • tools:listheader doesn't work although tools:listitem does
  • oppo, vivo app kill notification not coming in android fcm
  • OnClick Method could not function correctly with if else statement
  • Best method to download image from url in Android
  • How can I open Drawer Layout moving all the view and not just slide over?
  • how to show profile pictures of friends in a list view?
  • Creating TimePickerDialog with custom style in Android
  • Android 2.3.6 support library v7 option menu is missing
  • need of vblank in Display subssytem
  • Android MP4 stream - Video cannot be played
  • FastAdapter: Undo button does not show up after swipe
  • What are reasons to pass getActivity as the LifecycleOwner to the LiveData's observe method in a fragment?
  • Implement Face Recognition in Android
  • Titanium studio cannot run "titanium" command in terminal
  • How to execute command line ffmpeg commands programatically in android?
  • Make Android List Items clickable with each item pointing to a different link
  • listview runs successfully, but nothing is display in emulator
  • How to zoom in a bitmap with high resolution?
  • Passing listView data through intent
  • Google map route direction show by arrows in android v2
  • how to start "uiautomator test run" from android code?
  • Proguard in Android Library
  • Alarm Clock from GIT - Gives error - Android
  • Mock injection using Mockito - Android
  • Get Bitmap from universal image loader
  • Unable to include custom font in android
  • Basics on using logcat in Android development
  • Android: Clear cache programmatically
  • Android: pass object without serialization
  • Can not scroll and click item on gridview inside scrollview
  • Android device keeps disconnecting from adb / eclipse
  • Volley cannot create cache directory. Could not clean up file
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org