Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

android - Picasso first run not call onBitmapLoaded in for loop

I have problem with get bitmap use Picasso in for loop.

it is not called onBitmapLoaded in first run.

second run it called

for (int i = 0; i < 3; i++) { 
            final int k=i;
            Picasso.with(this)
                    .load(ListA.get(i).getImage()) //image
                    .resize(100, 100)
                    .transform(new ImageTrans_CircleTransform())
                    .into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {                         
                            Log.i("load", "Ok " + k);   
                           //use bitmap for add marker to map                
                        }

                        @Override
                        public void onBitmapFailed(Drawable errorDrawable) {
                        }

                        @Override
                        public void onPrepareLoad(Drawable placeHolderDrawable) {
                            Log.i("load", "first " + k);
                        }
                    });  
        }

Log

11-04 16:42:22.222  11677-11677/com.tenten I/load﹕ first___0 
11-04 16:42:22.222  11677-11677/com.tenten I/load﹕ first___1 
11-04 16:42:22.232  11677-11677/com.tenten I/load﹕ first___2 

I use picasso to get bitmap from list image.

I need bitmap not imageview.

Thank you. :D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A common issue with using Picasso's Targets is that people don't keep strong references to them. This results in the targets being randomly working, because sometimes they get collected by the GC before being finished, and sometimes they live long enough to get the callback called.

What you have to do is to store those callbacks somewhere until they are finished. Here's an example:

final List<Target> targets = new ArrayList<Target>();
for (int i = 0; i < 3; i++) { 
    final int k=i;
    Target target = new Target() {

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {                         
            Log.i("Targets", "Loaded: " + k);   
            targets.remove(this);                
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            targets.remove(this);
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            Log.i("Targets", "Preparing: " + k);
        }
    }
    targets.add(target);
    Picasso.with(this)
        .load(ListA.get(i).getImage()) // Start loading the current target
        .resize(100, 100)
        .into(target);  
}

To make sure the list does not get GC'd too, make targets a global variable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...