How to save a bitmap in memory

After Saving the Bitmap resource in a static variable , it seems that storing a static link to android.graphics.Bitmapin Viewmay leak the link to this first Viewone that created it. What is the idiomatic way to solve this problem in Android? I do not want to name BitmapFactory.decodeResource(resource, id)every time an instance of this view is created, because it will be done (many times) in each individual Activity. I want this little one to Bitmapalways be remembered. So what is the correct way to do the following:

public class MyView extends View {
    private static Bitmap star;
    public MyView(Context context) {
        synchronized(this) {
            if (star == null) {
                star = BitmapFactory.decodeResource(getResources(), R.drawable.star);
            }
        }
    }
    // ...
}
+4
source share
3 answers

, onPause(). recycle() . , , , onResume().

, , , , , 0. , onCreate()/onDestroy().

null .

+3

, . , . , .

.


, null. ( final)

0

why not just load the image into the action in which the views will be displayed, and transfer the bitmap to the views

or if you do this throughout the application, use the application context to download the image.

0
source

All Articles