onBitmapLoaded of Target object not called on first load
Solution 1:
As noted by the other respondents (@lukas and @mradzinski), Picasso only keeps a weak reference to the Target
object. While you can store a strong reference Target
in one of your classes, this can still be problematic if the Target
references a View
in any manner, since you'll effectively also be keeping a strong reference to that View
as well (which is one of the things that Picasso explicitly helps you avoid).
If you are in this situation, I'd recommend tagging the Target
to the View
:
final ImageView imageView = ... // The view Picasso is loading an image into
final Target target = new Target{...};
imageView.setTag(target);
This approach has the benefit of letting Picasso handle everything for you. It will manage the WeakReference
objects for each of your views - as soon as one is no longer needed, whatever Target
processing the image will also be released, so you're not stuck with memory leaks due to long-lived targets, but your Target will last as long as its view is alive.
Solution 2:
Picasso does not hold a strong reference to the Target object, thus it's being garbage collected and onBitmapLoaded
is not called.
The solution is quite simple, just make a strong reference to the Target
.
public class MyClass {
private Target mTarget = new Target() {...};
public void getPointMarkerFromUrl(final String url, final OnBitmapDescriptorRetrievedListener listener) {
Picasso.with(context)
.load(url)
.resize(maxSize, maxSize)
.into(mTarget);
}
}
Solution 3:
If I had ImageView I would simple make like this: imageView.setTag(target);
I use next solution for loading Bitmaps into notifications, so I need only bitmap.
So create Set witch will store Target objects and remove them on finish loading.
final Set<Target> protectedFromGarbageCollectorTargets = new HashSet<>();
private void loadBitmap(String url) {
Target bitmapTarget = new BitmapTarget(nEvent);
protectedFromGarbageCollectorTargets.add(bitmapTarget);
Picasso.with(context).load(url).into(bitmapTarget);
}
class BitmapTarget implements Target {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
//handle bitmap
protectedFromGarbageCollectorTargets.remove(this);
}
}
}
@Override
public void onBitmapFailed(Drawable drawable) {
protectedFromGarbageCollectorTargets.remove(this);
}
@Override
public void onPrepareLoad(Drawable drawable) {
}
}