I want to show a ViewPager
with ~50 pages, each containing a different image. Using Picasso, the first 20-25 pages work perfectly. However, at that point I get an OutOfMemoryError
, and no images are loading at all:
Throwing OutOfMemoryError "Failed to allocate a 7477932 byte allocation with 1932496 free bytes"
I'm using the following code in my PagerAdapter
:
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
View view = getView();
Picasso picasso = getImageLoader(mContext);
picasso.load(getUrl(position)).fit().into((ImageView) view.findViewById(R.id.imageview));
container.addView(view);
return view;
}
@Override
public void destroyItem(final ViewGroup container, final int position, final Object object) {
container.removeView((View) object);
}
What can I do to avoid this?
I have found this issue.
Some points noted:
skipMemoryCache()
builder.executor(Executors.newSingleThreadExecutor());
Picasso
: do not create a new instance using Picasso.Builder
every time.I managed to fix my problem with the last one.
This question is high ranked on Google hits, so I'm adding my solution to this problem.
Adding .fit()
solved the problem for me. I'm successfully loading images with below code.
picasso.load(PartyUtil.getPartyIconResourceFromFullPartyName(parties.get(position)))
.fit()
.into(holder.icon);
Removing .fit()
causes my application to throw an OutOfMemoryException
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With