How To Keep A Bitmap In Memory
Solution 1:
Create a static clean-up method in your View that you call from your Activity's onPause
(). In that call, call the bitmap's recycle()
and clear out the reference. Likewise, instead of creating the bitmap in the constructor, have an initialization call that you call in your activity's onResume()
.
If you have any concerns that there might be an overlap because your View is used across activities, you can have the init and clean-up calls maintain a reference count, so that the bitmap is only destroyed when the count hits 0. If the bitmap is small enough, you can consider onCreate()
/onDestroy()
as well.
Be sure to check the bitmap reference in your view class for null before using it.
Solution 2:
I just noticed the field is static. You should set a static field in a constructor as this is plain confusing. I suggest you have a static method which is called, which set the field.
You can make the static method synchronized.
Constructors are always thread safe, so you don't need to use synchronized or check for null. You can just set the fields (and make it final
)
Solution 3:
why not just load the image in the activity where the views will be and pass the bitmap to the views
or if your doing it over the whole application, use the application context to load the image.
Post a Comment for "How To Keep A Bitmap In Memory"