onBitmapLoaded of Target object not called on first load
Asked Answered
B

8

138

In my function :

public void getPointMarkerFromUrl(final String url, final OnBitmapDescriptorRetrievedListener listener) {
final int maxSize = context.getResources().getDimensionPixelSize(R.dimen.icon_max_size);
Target t = new Target() {
  @Override
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    if (bitmap != null)
      listener.bitmapRetrieved(getBitmapDescriptorInCache(url, bitmap));
    else
      loadDefaultMarker(listener);
  }

  @Override
  public void onBitmapFailed(Drawable errorDrawable) {
    loadDefaultMarker(listener);
  }

  @Override
  public void onPrepareLoad(Drawable placeHolderDrawable) {
  }
};

Picasso.with(context)
    .load(url)
    .resize(maxSize, maxSize)
    .into(t);
}

The onBitmapLoaded() is never called the first time I load pictures. I've read some topic like https://github.com/square/picasso/issues/39 which recommand to use fetch(Target t) method (it seems to be a problem of weak reference...), but this function is not available in the last release of picasso (2.3.2). I've only a fetch() method, but I cannot use into(mytarget) at the same time

Could you explain me how to use fetch() with a custom Target please ? Thank you.

Doc : http://square.github.io/picasso/javadoc/com/squareup/picasso/RequestCreator.html#fetch--

Burk answered 12/6, 2014 at 9:22 Comment(4)
make sure to use okhttp 2.0.0, I encounter same issue when using Picasso 2.3.2 with Okhttp 1.6.0Yeh
github.com/square/okhttp afaik, it is mandatory if you're using Picasso 2.3.2 to include okhttp (and okio) library. are you using eclipse or android studio ?Yeh
I'm using IntelliJ. I've seen my gradle dependencies, I didn't see okhttp... Picasso seems to work without itBurk
@Burk how did you implement below solution with the markers?Windpipe
C
263

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.

Cicala answered 13/11, 2014 at 21:27 Comment(6)
YES! I chose to create a parallel list of target (List<Target>) to keep all the target objects, but your solution is better. The target "will last as long as it's view is alive". Thanks.Burk
I don't have an image view, how can I solve this problem then? When dealing with this kind of situations, the gc is your worse enemyFogdog
You can even store it in an ArrayList<Target> and it will work, just remember to clear that arraylist :-)Cai
In onBitmapLoaded and onBitmapFailed, I am also doing imageView.setTag(null) after processing the bitmap. Is it not needed ?Brabazon
Shouldn't we nullify the tag in onBitmapLoaded / onBitmapFailed ?Middlemost
@Fogdog I don't have a view either so i have nothing to tag my target to. I know this is old, but did you find a solution?Deluna
C
65

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);
   }
}      
Calends answered 7/7, 2014 at 2:31 Comment(6)
Or make your View implement Target.Sacking
in the docs, it says you have to override Object.equals(Object) and Object.hashCode() methods. do you have a working sample?Ambrogino
where is it written ? I still have my problem even by making a strong reference to my Target().Burk
I have now installed okHttp, it's a bit faster to load but I still have the same problem at the first launch. Any ideas ?Burk
@psv: Did you get to solve picasso first launch issue? I have the same problem? If you have solved how did you solve it?Grith
@TheDevMan, nope it's not solved. Workaround solution I used : Save each target object in an ArrayList to avoid garbage collection. It's "dirty" and reduce performances but I don't have any other solution.Burk
S
27

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();
   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) {
 
        }
    }
Smother answered 6/6, 2015 at 9:52 Comment(2)
what is nEvent ? Target bitmapTarget = new BitmapTarget(nEvent);Kinzer
@Kinzer you can ignore it.Smother
T
13
ImageView profile = new ImageView(context);
        Picasso.with(context).load(URL).into(profile, new Callback() {
            @Override
            public void onSuccess() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {//You will get your bitmap here

                        Bitmap innerBitmap = ((BitmapDrawable) profile.getDrawable()).getBitmap();
                    }
                }, 100);
            }

            @Override
            public void onError() {

            }
        });
Tholos answered 18/2, 2016 at 11:42 Comment(4)
It's solved my problem too. i wanted use it with Notification. sometimes image was downloading with Target and sometimes not. but after using ImageView i was able to load images everytimeTransformation
in my case, except all, this was the best solution!Susanasusanetta
@RaveeshGS notification builder in android uses setLargeIcon(Bitmap) to set Image. I don't know how you use ImageView instead of setLargeIcon !?Kinzer
@Kinzer they are using imageview to just download the bitmap. After downloading you can use it wherever you want.Tholos
C
6

I encountered similar issue and holding reference to the target didn't helped at all so I used the following code which returns a Bitmap:


Bitmap bitmap = picasso.with(appContext).load(url).get();

on the down side -> there's no callback and you can't call this function on the main thread, you have to run this function on a background thread as in the following example:


handlerThread = new HandlerThread(HANDLER_THREAD_NAME);
handlerThread.start();

Handler handler = new Handler(handlerThread.getLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
        Bitmap bitmap = null;
        try {
            bitmap = picasso.with(appContext).load(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bitmap != null) {
                //do whatever you wanna do with the picture.
                //for me it was using my own cache
                imageCaching.cacheImage(imageId, bitmap);
            }
        }
    }
});

Another thing that works a whole lot better is just using Glide!

I needed to use both of them since the purpose of my project was to use 2 different image downloading api's to show an images gallery and to give the user the ability to choose which api to use.

I have to say, I was amazed by the results, Glide's api worked flawlessly in every aspect (Glide's target doesn't have weak reference) wile Picasso gave me hell (that was my first time using Glide, I usually used Picasso so far, seems like today it's gonna change ^^ ).

Caravansary answered 22/7, 2016 at 17:18 Comment(1)
I've tried lots of solutions, on the first try using Glide it downloaded everything correctlyFlute
V
5

Here is the solution for those who aren't using a view. This helper method uses a list to temporarily store the target object until a result is returned so that it isn't gc'd:

private List<Target> targets = new ArrayList<>();

public void downloadBitmap(final Context context, final String url, final MyCallback callback) {
    Target target = new Target() {

        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            targets.clear();
            callback.onSuccess(bitmap);
        }

        @Override
        public void onBitmapFailed(Exception e, Drawable errorDrawable) {
            targets.clear();
            callback.onFailure(null);
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
        }
    };
    targets.add(target);
    Picasso.with(context).load(url).into(target);
}
Vmail answered 1/2, 2018 at 21:11 Comment(0)
S
3

Like @lukas said (and quoting), Picasso does not hold a strong reference to the Target object. To avoid garbage collection you must hold a strong reference to the object.

About fetch() method. It's pretty clear in the documentation that fetch() is not to be used with an ImageView nor a Target, it's just to "warm" up the cache and nothing else, so you're not going to be able to use it the way you want.

I recommend you holding a strong reference like @lukas explained, it should work. If not please open up a new issue on the GitHub page of the project.

Spitter answered 21/8, 2014 at 3:5 Comment(0)
T
0

I had faced same issue but when I change the dependency as below mentioned, It works properly now.

 implementation 'com.squareup.picasso:picasso:2.5.2'
 implementation 'com.squareup.okhttp:okhttp:2.3.0'
 implementation 'com.squareup.okhttp:okhttp-urlconnection:2.3.0'
Trifocals answered 24/6, 2020 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.