I'm getting an Out Of Memory exception using Picasso when loading "large" images (> 1.5MB) from Android Gallery (using startActivityForResult).
I'm using a custom Target object because I need to preprocess the Bitmap when it is ready and also I'm using a custom Transform object to scale the Bitmap.
The problem is that the method public Bitmap transform(Bitmap source)
on my Transform object is never called because the Out Of Memory Exception, so I don't get the oportunity to resample the image.
But, if I use the .resize(maxWidth, maxHeight)
method, then it loads the image OK. I supossed that the Transform object was for that purpose too, but it seems that the transform method is called after the resize, and, if I don't call resize, then it will end in an Out of Memory..
The problem is that with resize I need to specify both width and height, but I need to scale and keep the aspect ratio.
Consider that the images will be selected from user Gallery, so they can be bigger or smaller, portrait, squared or landscape, etc, so I need my own Transformation object to perform the logic that needs my application.
Best Answer
I found a solution..
In my Transform object I needed to scale the image (keeping aspect ratio) to 1024 x 768 max.
Transform object was never called unless I call
.resize(width, height)
to resample down the image.For keeping aspect ratio and using resize I call
.centerInside()
. This way image will be scaled resample to fit width, height).The value that I give to .resize(width, height) is
Math.ceil(Math.sqrt(1024 * 768))
. This way I'm sure to have an image higher enough for my custom Transform object, and also avoid Out Of Memory exceptionUpdate: Full example
Following this example you will get a image that fits inside MAX_WIDTH and MAX_HEIGHT bounds (keeping aspect ratio)
And this is my custom BitmapTransform class: