Search code examples
androidgridviewbitmappicasso

"Out of memory" when using Picasso to download images and put images into GridView


I have an app which loads certain Images into a Grid View. The problem is, that, after 2 or 3 images are loaded the following error appears:

E/dalvikvm-heap﹕ Out of memory on a 7056016-byte allocation.

I have read on other Stackoverflow related questions, and some of the solutions consisted in using .fit() and .resize() on a Bitmap to solve this problem.

The problem is, that i am downloading the images directly from the URL.

Can you suggest me an approach to solve this OOM problem?

Cheers!

Here is my GridView adapter code:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    List<ParseObject> urlList;
    private LayoutInflater inflater;

public ImageAdapter(Context c, List<ParseObject> urlList) {
    mContext = c;
    this.urlList = urlList;
    inflater = LayoutInflater.from(c);
}
public int getCount() {
    return urlList.size();
}
public Object getItem(int position) {
    return null;
}
public long getItemId(int position) {
    return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View view, ViewGroup parent) {

    View gridItem = view;
    ViewHolder viewHolder;

    if (gridItem == null) {
        viewHolder = new ViewHolder();
        gridItem = inflater.inflate(R.layout.grid_view_item, parent, false);
        viewHolder.squareImageView = (SquareImageView) gridItem.findViewById(R.id.gridItem_squareImageView);
        viewHolder.progressBar = (ProgressBar) gridItem.findViewById(R.id.gridItem_progressBar);
        gridItem.setTag(viewHolder);
    } else {
        viewHolder= (ViewHolder) view.getTag();
    }
   Picasso.with(mContext).load(urlList.get(position).get("thumbnailURL").toString()).into(viewHolder.squareImageView);
    viewHolder.progressBar.setVisibility(View.INVISIBLE);
    return gridItem;
}

class ViewHolder {

    SquareImageView squareImageView;
    ProgressBar progressBar;

    }
} 

Solution

  • try this it will use less memory. Before loading the image it resize it

    Picasso.with(mContext).
    load(urlList.get(position).get("thumbnailURL").toString())
    .resize(100,100)
    .into(viewHolder.squareImageView);