0

I am trying to load multiple bitmaps from url using Picasso, and draw the bitmaps I load on my google map.

all of this code is in a google MapFragment.

public void drawLocations(final UserPictureUrl[] userPicsArray) {
    for (int j = 0; j < userPicsArray.length; j++) {
        Location targetLocation = ... // getting location

        // userPicsArray[j].getPicUrl() is the bitmap url
        loadProfilePicture(userPicsArray[j].getPicUrl(), targetLocation);
    }
}

private void loadProfilePicture(String picUrl, final Location location) {
    friendLoadTarget = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Toast.makeText(getActivity(), String.valueOf(counter), Toast.LENGTH_SHORT).show();
                counter++;
                int width = 40;
                int height = 40; getResources().getDimension(R.dimen.profile_height);
                // drawCanvas draws the bitmap to the map
                drawCanvas(location, Bitmap.createScaledBitmap(bitmap, width, height, false), picType);
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        };
        Picasso.with(getActivity()).load(picUrl).into(friendLoadTarget);
}

My problem is - I have 3 bitmaps to load, but when running the app, the behavior of the loading is unexpected - sometimes it loads only 1 bitmap, sometimes all 3, sometimes 2 and sometimes none.

As you can see, I put a Toast to test if the method is actually called, and I almost never get all 3 Toast messages. sometimes only one message with "1" and that's it..

How can make sure all images are loaded properly?

Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
  • Just to make sure, are you keeping that reference to the Target around somewhere? Picasso only holds a WeakReference to Targets. Also, is onBitmapFailed getting called? – Eric Cochran Feb 10 '16 at 01:59
  • 1
    Actually, that's it. You are probably replacing your Target reference, so you are only getting the last one to get called back. The others are gc'd. I would propose making your View or something else that stays around as long as the View implement the Target interface. – Eric Cochran Feb 10 '16 at 02:00
  • thanks! it really was that issue. ended up having an array to hold the targets references. – Ofek Agmon Feb 10 '16 at 08:22

1 Answers1

2

A Picasso only holds weak references to Targets. You are losing the first two references when you loop there. You'll need to keep those references around for as long as you want to load the Targets.

Eric Cochran
  • 8,414
  • 5
  • 50
  • 91