angry_coder
angry_coder

Reputation: 1

I want to know better approaches on how to pass bitmaps in different activities

I have the following case:

Activity1

public class Activity1 extends Activity{
  ImageUtils.setArt(bitmap);
}

Activity2

public class Activity2 extends Activity{
  Bitmap b= ImageUtils.getArt();
}

ImageUtils

public class ImageUtils{
  private static Bitmap mArtWork;
  public static Bitmap getArt(){
    return mArtWork;
}

public static void setArt(Bitmap art){
  this.mArtWokr=art;
}

Now, I need a good approach, to share Bitmaps among different Activitys.

Since static variables will not be Garbage Collected during my application execution, I need a good way to share the images amongst my scenarios.

I am considering using a singleton "ImageUtils" to hold my images, and then access then.

I have considered and disregarded passing pareaceable bitmaps, and even sharing URI through intents, but those have had bad results.

Are there better approaches?

Upvotes: 0

Views: 66

Answers (2)

Chenmin
Chenmin

Reputation: 121

Try to use Memory Cache (LRU cache for example) or Disk cache (File I/O for example). This is the suggested way by Google about how to caching bitmaps. The google documents is here.

  1. Save the bitmap in the cache in your Activity1
  2. Get the bitmap in the cache in Activity2, if its null, download it again

Set size of yur LRU cache. If the whole images size exceed the maiximun of the LRU cache you set, the least recently used images will be deleted.That's how LRU cache works. While if you save the images on the disk, don't forget to delete the images when you finish using them.

Upvotes: 1

RRTW
RRTW

Reputation: 3190

It's not a good idea to pass a bitmap through memory, because bitmap isn't been compress in memory. Maybe try to save it into a temp file, and load in another activity...

Upvotes: 0

Related Questions