5

I am trying to set an ImageView in a Google Maps Marker's InfoWindow and have copied the code from this answer pretty exactly, except that my InfoWindowAdapter isn't an anonymous inner class (it's just an inner class of the activity). This was working before, but for some reason it has stopped working - the onSuccess method in the Callback doesn't get called, so the InfoWindow only displays the image the second time it is opened.

Looking at the logs for Picasso I'm getting messages similar to Main canceled [R20]+374ms target got garbage collected. I figured this might be because the Callback is getting gc'd, and tried making it final, and also saving the object in a class field (neither of these worked, although maybe I was doing it wrong?)

What could be happening here, and how can I fix it? Is that target in the error message referring to the Callback, or could it be referring to the marker that gets passed as an argument to the Callback's constructor?

Another odd thing is that sometimes the images are loaded correctly when the InfoWindow is first opened - I'm trying to find out why, but basically I have a lot of markers and whether their images load correctly or not on the first go seems to be inconsistent. There are some (the majority) that never seem to load correctly when the InfoWindow is first opened.

[edit] This was after a bunch of code was merged into that activity, so could it be a memory thing? (there's more processing done now than there was when I wasn't having this problem)

[edit 2] I'm having exactly the same issue with Glide!! Probably garbage collection?

3 Answers 3

8

I'm not familiar with that answer, but Target could be gc'ed when you do not hold strong reference to that.

It's because Picasso holds Target instance with weak reference.

You should hold Target instance somewhere outside of Picasso.

Check this issue: https://github.com/square/picasso/issues/352

1
  • Thanks, I tried storing the Callback object in a field in the activity (both the Callback and the InfoWindowAdapter are nested in it) and then passing that field as an argument to .into() but I get the same messages and issue (not sure if I was going about that right)edit Apparently Callbacks are kept as a strong reference anyway Mar 26, 2015 at 9:34
2

Solved it, the garbage collection message was actually referencing the ImageView, not the Callback object. Ensuring that the ImageView object isn't garbage collected will correct this (e.g. by saving the ImageView in a field in the class, or even the activity that my class was nested in)

0

I was doing the same mistake, here was the solution that worked :

My previous code :

        picasso.load(url).into(new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {

    }

    @Override
    public void onBitmapFailed(Exception e, Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
});

After this I just created a new variable for Target object :

final Target target=new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        imageView.setBackground(new BitmapDrawable(mContext.getResources(), bitmap));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    }

    @Override
    public void onBitmapFailed(Exception e, Drawable errorDrawable) {

    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
};

then I used the target object later in my code :

        picasso.load(url).into( target);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.