kmn
kmn

Reputation: 2655

Loading, recycling and dismissing bitmaps for memory efficiency in Android

I've been trying to solve OutOfMemoryError Crashes for a while now.

The Problem

I know where the memory use is coming from : - My App uses the same background image on every activity - I use the MPAndroidChart Library for charts - Other icons and bitmaps from our graphic designers.

I've looked at the memory use view in Android Studio. When I go from Activity A to activity B, memory goes from (example) 40 to 60 mb Then when I go back to activity A from activity B, it goes up again from 60 to 80

The Question (s)

Update

I've Already read Android's developer documentation for efficiently loading bitmaps :

https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

But it doesn't say how to remove images from memory when leaving an activity

Upvotes: 2

Views: 259

Answers (1)

rhlin888
rhlin888

Reputation: 225

I am kind of new to Android, but this is how I fixed my OutOfMemory:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;
        BitmapDrawable background = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.image, options));
        background.setTileModeX(android.graphics.Shader.TileMode.REPEAT);
        bar.setBackgroundDrawable(background);

I found this somewhere, but I don't remember where.

Upvotes: 2

Related Questions