0

What is the best way to load an image inside an ImageView? I'm currently using .setImageResource() to do this, but i've heard that '.setBitmap()' is better, as you can make it not load the entire image in the memory before it resizes it. This post: Strange out of memory issue while loading an image to a Bitmap object

Which is the best, out of a device memory point of view? Which occupies less memory, which "stresses" the device the least?

Community
  • 1
  • 1
AndreiBogdan
  • 10,858
  • 13
  • 58
  • 106

2 Answers2

2

Placing your images in the imageview after re-sizing it is the best as it would avoid memory problems. setBitmap() is better.

If you have just one or two images, then .setImageResource would suffice. But if u have more number of images of varying sizes the setBitmap() is better.

Which is the best, out of a device memory point of view? - same one.. Which occupies less memory, which "stresses" the device the least? - same one!

pixelscreen
  • 1,945
  • 2
  • 18
  • 40
  • And from what I've read, it seems that you need to remove the bitmap from memory manually, it is not done automatically bu the GC. Is this true? That will be a pain in my ars if so :( – AndreiBogdan Jul 18 '12 at 14:24
  • Not needed. The android system takes care of garbage collection automatically. If really needed, you may call android.system.gc(); although I think that wouldn't be necessary at all. – pixelscreen Jul 18 '12 at 14:25
2

Generally speaking, try not to use setImageResource() for larger images as this is executed on the UI thread (which may cause UI lag).

As for loading Bitmaps, I highly recommend this guide from Google. It covers asynchronous loading, using caches, general loading and much more. The tutorial came out within the last few months so uses the current best practices.

Ljdawson
  • 12,091
  • 11
  • 45
  • 60