Home > Tricks & Tips > Copy Bitmap object to avoid references

Copy Bitmap object to avoid references


If you assign value of a Bitmap to the other one, it means you have create a reference to the source Bitmap. Hence, if either one is recycled, the other one certainly reference to the already-recycled Bitmap object in memory, which might cause some problems like: Exception on Bitmap, throwIfRecycled.

In some case, you probably want to copy a Bitmap object to a new one, here the trick:

Bitmap src = .....<allocated the source Bitmap>
Bitmap newBitmap = src.copy(src.getConfig(), src.isMutable() ? true : false);

Pretty much simple!
@Remember to recycle each one after used.

Cheers,
Pete Houston

  1. Joe
    March 29, 2015 at 9:33 pm

    Good tip 🙂
    Just one little thing, this line is not necessary: src.isMutable() ? true : false
    you can just use src.isMutable() as it returns a boolean.

  2. October 12, 2012 at 7:27 pm

    Great, thanks

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: