Search code examples
androidout-of-memoryviewflipper

'OutOfMemoryError' while adding multiple images to ImageViews inside ViewFlipper?


I have a fragment contains 6 ViewFlippers, each flipper contains at least 10 ImageViews. I tried to load all the images and put them in the ImageViews but the restricted memory didn't let me to do that. Every time I run the application this exception occurs.

when I reduce number of images my app runs, but that's not a solution.
So what's the best way for doing that?

Here what I tried so far:

for(int i=0; i<flippers.length; i++) {
    for (int j = 0; j < names.length; j++) {
        int imageResource = getResources().getIdentifier(names[j], "drawable", rootView.getContext().getPackageName());
        ImageView imageView = new ImageView(rootView.getContext());
        imageView.setImageResource(imageResource);
        flippers[i].addView(imageView);
    }
}

26546-26546/com.azad E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.azad, PID: 26546 java.lang.OutOfMemoryError


Solution

  • Make use of library for resizing the image before loading

    Some of the library you can use are

    Picasso

    UIL

    This library will help you to cache the image. And when the image is not required you can clear it from memory