简体   繁体   中英

onBitmapLoaded never called

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);
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM