6

I am loading image to image-view from JSon. JSon only bring the path of image URL. I am setting the value using picasso. but it gives error for some image and for rest it is working fine.

Picasso.with(context).load(rowItem.getProductImages().get(0)).into(holder.productImageView);

error is :

 2771-2793/com.koove E/art﹕ Throwing OutOfMemoryError "Failed to allocate a 31961100 byte allocation with 4194304 free bytes and 27MB until OOM"
03-25 09:53:23.666    2771-2793/com.koove D/skia﹕ --- decoder->decode returned false
duggu
  • 37,851
  • 12
  • 116
  • 113
Vikram singh
  • 343
  • 1
  • 3
  • 15
  • 1
    You need to download the images in the background, then resize them (or make a copy of one) to the screen size / presentation size (again in the background) then display them. – Tigger Mar 25 '15 at 05:40
  • 2
    possible duplicate of [How to load large images in Android and avoiding the out of memory error?](http://stackoverflow.com/questions/21392972/how-to-load-large-images-in-android-and-avoiding-the-out-of-memory-error) – Tigger Mar 25 '15 at 05:43

1 Answers1

5

You should use the method fit() in Picasso, it's measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView.

The advantage is that the image is at the lowest possible resolution, without affecting its quality. A lower resolution means less data to be hold in the cache.

Picasso.with(context).load(rowItem.getProductImages().get(0)).fit().into(holder.productImageView);

If you still have the OOM, remove the cache using the memory policy.

Picasso.with(context).load(rowItem.getProductImages().get(0)).memoryPolicy(MemoryPolicy.NO_CACHE).fit().into(holder.productImageView);
Lilo
  • 2,724
  • 25
  • 16
  • Great answer. fit() really works for me. Just want to know can I handle memoryPolicy() condition on if(fit() == false) condition? – VVB Feb 15 '16 at 09:46
  • I don't think so, the fit() method return a RequestCreator object. Since using fit() can delay the image request, you can use instead the resize() and onlyScaleDown() methods, then, the image will be resized only if it's bigger than resize parameters. Here a great link for Picasso usage methods https://futurestud.io/blog/picasso-image-resizing-scaling-and-fit – Lilo Feb 15 '16 at 10:31
  • Okay. Thank you for adding valuable inputs. It would be great if you could just mention the reason why this question arises? in one line. – VVB Feb 15 '16 at 13:26