2

I have a layout which I'm adding ImageViews (about 13) to at run time using a single thread. When I load more than 7 or 8 images (970px x 970px, 270kb) the application crashes displaying an out of memory error. Now when I load the same image rather than choosing 13 different images it displays fine. And also when I reduce the size of the images (16px x 16px, 770bytes) it displays fine.

So does anyone know what the max memory allowed when loading images? What is the best way to get around this issue? Obviously people have come across this issue before and solved it due to loading image galleries etc.. what's the best way to load these images into the application? Any help would be greatly appreciated.

The following is how I'm reading the image from the resources. This is within a loop that I am taking the "drawableName" from a list of objects that I'm reading through. This is all within the onPostExecute of a thread (i.e. new AsyncTask())

String defType = "drawable";
String defPackage = this.getPackageName();
int resourceId = Activity.this.getResources().getIdentifier(drawableName, defType, defPackage);
((ImageView) view.findViewById(R.id.iv_image)).setImageResource(resourceId);
Graham Baitson
  • 580
  • 1
  • 11
  • 29
  • http://stackoverflow.com/questions/16765899/out-of-memory-error-with-bitmap/16766123#16766123. check this if it helps – Raghunandan Jan 14 '14 at 13:23
  • these images are quite big ... – njzk2 Jan 14 '14 at 13:24
  • Thanks guys, I'll check out that link Raghunandan. njzk2, what would be the average size do you think for an image to compatible across all screen sizes? Suppose the actual size doesn't really make too much of a difference as the number of images you are loading would reflect this (e.g. loading 100 small images could be the same as loading 10 big images). – Graham Baitson Jan 14 '14 at 13:29

2 Answers2

6

The fastest way to resolve your problem is to use the Picasso library from Jake Wharton. It enables in one line of code to load your image and manage caching, transformations, download etc..

Here is the link to download the library: http://square.github.io/picasso/

All the code you need is :

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

It should work like a charm :)

aurelien_lepage
  • 261
  • 2
  • 8
4

I would try by loading only the images that are going to be shown and resizing them to an appropiate size. There are guides in the android docs about displaying images efficiently read here

You can also checkout Picasso library http://square.github.io/picasso/ wich I find very useful for these kinds of tasks.

Federico Perez
  • 976
  • 1
  • 13
  • 34