0

After strugling for days with this problem I finally found a way to do it.

Q: How to show a large bitmap without carshing the application on VM budget by turning the phone around or open the Activity the second time?

Magakahn
  • 498
  • 9
  • 31

2 Answers2

1

Always call bitmap.recycle() method after using Bitmaps. Calling GC is not the solution, it is not guaranteed that it will be called. And also, without calling recycle method, the bitmap will not get freed after its use.

options.inScaled = true;
options.inPurgeable = true;
options.inInputSharable = true;

If this also doesnt solve your problem, try ussing WeakReferences: Bitmap, Bitmap.recycle(), WeakReferences, and Garbage Collection

If this also does not work, then there might be some other memory leak in your code. Use MAT in eclipse to find out the memory leak from your code.

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • The problem is that if you call bitmap.recycle() is that it then carshes because it can not find the Bitmap after it has been recycled. – Magakahn Jun 05 '12 at 10:27
  • Call this method only if(bitmap != null) – Shrikant Ballal Jun 05 '12 at 10:32
  • Why are you calling it in onPause(), I would recommend you to call this method after this -> myLinearLayout.setBackgroundDrawable(d); statement itself. – Shrikant Ballal Jun 05 '12 at 10:34
  • My code is not optimal. Read the question. I published this to help others with the same problem. Thanks for impoving sugestions! – Magakahn Jun 05 '12 at 10:38
  • This is a share knowledge question. As you may lotice I have already solved the problem and wanted to help other in the same situation googleung this question. I am not testing these improvements myself. – Magakahn Oct 17 '12 at 08:09
0

You have to downscale the picture a bit before displaying it using:

BitmapFactory.Options options = new BitmapFactory.Options();
   
    options.inSampleSize = 3; // If number equals 3, it is only showing one third of all pixels. May crash if you are using 2, but 2 works in most cases. 
   
    Bitmap bMap = BitmapFactory.decodeFile((sdcard/exaple_file)), options);
 
   
   
 Drawable d =new BitmapDrawable(bMap);
   myLinearLayout.setBackgroundDrawable(d);

And in onPause:

@Override
    protected void onPause() {
    super.onPause();
  

   System.gc();
    }

// Some say that you should never call system gc your self, but for me it helps with stability.

This solved my problem, but may not be 100% optimal. But it stops the App crash on VM budget.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Magakahn
  • 498
  • 9
  • 31