0

I am loading images into memory in android phone so I can pass them into a web service through a webservice.

Sometimes i get out of memory error, i can already catch this exception with

    try{
        bm = BitmapFactory.decodeFile(path);
    }catch(OutOfMemoryError ofmE){
        System.out.println("OUT OF MEMORY $$$$$$$$" + ofmE);
    }

Although i need to execute this operation, so i need to clean the ram memory. How can i do this? programatically i mean.

  • 1
    Loading large bitmaps into memory is a classic problem. You have to scale them down: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html Otherwise, look for a way to pass the file without decoding it. – NigelK Jan 28 '14 at 14:37
  • That is true, but as small as it can be it might always give you a outofmemory exception. – JoaoFilipeClementeMartins Jan 28 '14 at 14:38
  • the size of the file doesn't matter, it's how big it is when loaded in to memory.. you can half the size by using RGB_565 instead of ARGB_8888 (the default), for example - see the link that @NigelK posted – Ben Pearson Jan 28 '14 at 14:47

2 Answers2

0

You can use Picasso. It's the cleaner way.

Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
  • That is too much of a overhead, just to clean ram memory. I certainly don't need to add a new library, because this is a part of a large project. – JoaoFilipeClementeMartins Jan 28 '14 at 14:32
  • 1
    You can use `System.gc();` but with that you don't fix the problem. You need to `recycle()` the unnused bitmaps and resize the image before create the bitmap. Trust me, use Picasso, you will be happier. – Brais Gabin Jan 28 '14 at 14:39
0

Have you noticed the method decodeFile(String pathName, BitmapFactory.Options opts)? Use options to scale down the image size for saving memory. Refer to BitmapFactory.Options. public int inSampleSize If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. For example, you can set inSampleSize = 4. To recycle bitmap, you can use bm.recycle();

yushulx
  • 11,695
  • 8
  • 37
  • 64