3

In my app I want to swap images at runtime when user clicks on it.

there are two imageviews when user click on first image and then click on second image at the same time I'm fetching bitmap of first imageview's image and assigning to second imageview for this I used following code:

public Bitmap createBitmap(ImageView imageview) {
    imageview.setDrawingCacheEnabled(true);
    imageview.buildDrawingCache(false);

    if(imageview.getDrawingCache() != null) {
        Bitmap  bitmap = Bitmap.createBitmap(imageview.getDrawingCache());
        imageview.setDrawingCacheEnabled(false);
        return bitmap;
    } else {
        return null;
    }
}

Code is working fine but the cache not cleared every time and the bitmap created with previous cache so how I can clear a bitmap cache?

AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
Prachi
  • 2,559
  • 4
  • 25
  • 37

2 Answers2

2

This is a sample e.g. where i use to Free the native object associated with this bitmap.

Bitmap  bitmap;

public Bitmap createBitmap(ImageView imageview) {
    if (bitmap != null) {
        bitmap.recycle();
        bitmap = null;
    }
    bitmap = Bitmap.createBitmap(imageview.getDrawingCache());
    // Your Code of bitmap Follows here
}

Before use of Bitmap just free the object.

AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
Bhavin
  • 6,020
  • 4
  • 24
  • 26
1

use bitmap.recycle(); before valuating your bitmaps to clear their cache before recreating it.

Bob
  • 22,810
  • 38
  • 143
  • 225