1

I've got an issue with onBitmapLoaded. The method is not called when it should be (it is called the second time i enter my view). Nevertheless i keep a reference to my target since i add it to an arraylist.

I don't understand why it's not working. Does someone have an idea ?

public void loadBitmap() {

    if(loadtarget == null) {
        loadtarget = new Target(){

            @Override
            public void onPrepareLoad(Drawable arg0) {
                Log.d("Bitmap","On prepare load");
                targetList.remove(this);
                return;
            }

            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Log.d("Bitmap","OKAY for :" + filename);
                targetList.remove(this);
                handleLoadedBitmap(bitmap);
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {
                Log.d("Bitmap","Error for :" + filename);
            }
        };
    }
    targetList.add(loadtarget);
    Picasso.with(context).load(imageUrl).into(loadtarget);
}
Smittey
  • 2,475
  • 10
  • 28
  • 35
Tibo
  • 11
  • 1
  • Where is loadtarget declared? As a class attribute? – Christopher Nov 09 '15 at 10:47
  • Furthermore you have to override equals() and hashCode()-method. See JavaDoc: Objects implementing this class must have a working implementation of * {@link Object#equals(Object)} and {@link Object#hashCode()} for proper storage internally. – Christopher Nov 09 '15 at 10:49
  • Yeah, loadtarget is a class attribute – Tibo Nov 09 '15 at 10:50
  • From what i read, i thought i just had to override the three method of the target interface. – Tibo Nov 09 '15 at 10:58
  • Possible duplicate of [onBitmapLoaded of Target object not called on first load](http://stackoverflow.com/questions/24180805/onbitmaploaded-of-target-object-not-called-on-first-load) – trav3ll3r Nov 28 '16 at 05:02

2 Answers2

1

If targetList and loadtarget are both local variables then they will be marked for GC collecting as soon as the method finishes. Make sure targetList is a class variable so that its outlives the method.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • targetList and loadTarget are both class variables – Tibo Nov 09 '15 at 11:03
  • You should remove: `targetList.remove(this);` from `onPrepareLoad()` and add it to `onBitmapFailed()` – Daniel Zolnai Nov 09 '15 at 11:34
  • I noticed it after posting my question so i removed it but it didn't changed anything. onBitmapLoad is never called anyway. – Tibo Nov 09 '15 at 11:37
  • I'm talking about `onPrepareLoad` not `onBitmapLoad` – Daniel Zolnai Nov 09 '15 at 11:38
  • I understood, i changed my code. targetList.remove(this) has been removed from onPrepareLoad() but nothing changed. – Tibo Nov 09 '15 at 11:40
  • D/Bitmap: On prepare load E/ViewRootImpl: sendUserActionEvent() mView == null D/dalvikvm: GC_FOR_ALLOC freed 696K, 7% free 27490K/29484K, paused 18ms, total 18ms D/dalvikvm: GC_FOR_ALLOC freed 152K, 7% free 28291K/30324K, paused 18ms, total 19ms D/dalvikvm: GC_FOR_ALLOC freed 82K, 7% free 29158K/31164K, paused 15ms, total 15ms D/dalvikvm: GC_FOR_ALLOC freed 471K, 7% free 32040K/34104K, paused 20ms, total 20ms – Tibo Nov 09 '15 at 13:01
  • Thank for taking time to answer me :) – Tibo Nov 09 '15 at 15:53
  • New informations : i forgot to tell you that i was calling my loadBitmap in a loop. The function work fine if it is called once but seems to be in trouble when called in a loop. Does this give new ideas ? – Tibo Nov 12 '15 at 09:22
  • You are reusing loadtarget then, which might not be what Picasso expected. Try with removing this line: `if(loadtarget == null) {` and the closing `}` so you create a new one each time. – Daniel Zolnai Nov 12 '15 at 09:39
0

I've find some kind of trick to solve my problem.

By replacing : Picasso.with(context).load(imageUrl).into(targetList.get(i));

With :

Picasso.with(context).load(imageUrl).transform(new Transformation() {

            @Override
            public Bitmap transform(Bitmap source) {
                handleLoadedBitmap(source);
                return source;
            }
            @Override
            public String key() {
                return "";
            }
        }).into(imageView); // imageView is a fictive imageView allocated only for this operation

my code is working. I'm not sure that it's the best solution but it fixed my problem.

Community
  • 1
  • 1
Tibo
  • 11
  • 1