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);
3
  • stackoverflow.com/questions/16765899/…. check this if it helps Commented Jan 14, 2014 at 13:23
  • these images are quite big ... Commented Jan 14, 2014 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). Commented Jan 14, 2014 at 13:29

2 Answers 2

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

Got the solution i using this library 250 images load from res fast way...!! Thanks
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.