Search code examples
androidandroid-imagepicasso

Downloading a lot of images with Picasso - Out of memory


I'm currently using Picasso to download of images like that:

ImageView imageView = (ImageView) rootView.findViewById(R.id.imagePreview);
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

Transformation transformation = new Transformation() {

    @Override public Bitmap transform(Bitmap source) {
        int targetHeight = 500;

        double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
        int targetWidth = (int) (targetHeight * aspectRatio);
        Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
        if (result != source) {
            // Same bitmap is returned if sizes are the same
            source.recycle();
        }
        return result;
     }

     @Override public String key() {
         return "transformation" + " desiredWidth";
     }
};

RequestCreator requestCreator;
Picasso picasso = Picasso.with(context);
requestCreator = picasso
                    .load(resource)
                    .transform(transformation)
                    .skipMemoryCache()
                    .error(R.drawable.ic_placeholder_ricerca);

I'm even resizing the images but it's still not enough. The problem is that I have an endless listView, so I keep downloading images and after a will the memory get full. Any idea of how to prevent the problem? I thought to enlarge the memoryCache (in the code I have also tried to skip completely but without results), but I have no idea of how to do that (the code example online don't work :( ).


Solution

  • The problem was that I was caching myself the images thinking to make stuff faster, instead I was crowding the heap for no reason since Picasso handles itself this problem. So, just erased the optimizations and here we go.