4

I am using Picasso to load images from my server and display them in ImageView.

I observes some crash reports from user's phone where Out of Memory Exception happens when Picasso is trying to load an image into ImageView.

The stack trace as below:

java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832)
at android.content.res.Resources.loadDrawable(Resources.java:2988)
at android.content.res.Resources.getDrawable(Resources.java:1558)
at android.widget.ImageView.resolveUri(ImageView.java:646)
at android.widget.ImageView.setImageResource(ImageView.java:375)
at com.squareup.picasso.PicassoDrawable.setPlaceholder(PicassoDrawable.java:62)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:520)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:462)
at com.mycompany.myAdapter.getView(MyAdapter.java:102)
at android.widget.AbsListView.obtainView(AbsListView.java:2608)
at android.widget.GridView.makeAndAddView(GridView.java:1346)

The Code near MyAdapter.java:102 as below:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

    .....Some code .....

    Picasso.with(mContext)
    .load(url_to_server)
    .placeholder(R.drawable.default_placeholder)
    .into(holder.imageItem);  // Line 102

    .....Some code .....

    return convertView;
    }

Is there sth wrong with the way I use this library?

Calvin
  • 3,302
  • 2
  • 32
  • 41
  • it's at the `setPlaceholder`, which would indicate that `R.drawable.default_placeholder` is a very large image? – njzk2 Jul 23 '14 at 17:17
  • It's a 270x200 1.9KB png file. (But let's assume it is large, shouldn't Picasso manage that?) – Calvin Jul 23 '14 at 17:19
  • 1
    side note: 1.9k is the compressed size, which is irrelevant. the size in memory is 270*200*4 = 216KB. This is not very big, though. Possibly this is more the symptom of another memory issue, as when the memory gets filled, only the last allocation causes a crash. – njzk2 Jul 23 '14 at 17:22
  • 2
    are you loading a large image into your `ImageView`? if so, try resizing it. use `Picasso.with(context).load(bitmapUrl).resize(width, height).centerInside().into(imageView)` – chip Jul 28 '14 at 08:46

2 Answers2

4

To load small images like icons o launchers we can use .setImageResource(image) However, to load big images is better to use Picasso and get the benefit of simplicity and local caching.

It is necesary to setup Picasso properly according to the documentation:

"Be sure to use fit() to resize the image before loading into the ImageView. Otherwise, you will consume extra memory, experience sluggish scrolling, or encounter out of memory issues if you render a lot of pictures".

if (imageUri != null && !imageUri.isEmpty()) {
   Picasso.with(context).load(imageUri)
  .fit().centerCrop()
  .placeholder(R.drawable.user_placeholder)
  .error(R.drawable.user_placeholder_error)
  .into(imageView);
}
yaircarreno
  • 4,002
  • 1
  • 34
  • 32
0

Convert Your image to bitmap and load image efficiently

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html and check this url too Strange out of memory issue while loading an image to a Bitmap object

if you are using any delay while loading images(eg: timer) please check it

i hope it will helps you...

Community
  • 1
  • 1
Sarath Kumar
  • 1,922
  • 3
  • 19
  • 43