Fernando Santiago
Fernando Santiago

Reputation: 2288

Loading multiple images with Picasso on background

I am trying to get loaded an array of 20 urls in background with Picasso. So far i have the next code:

Log.d("GAME", "Loading all images");
for (int i = gamePieces.length-1; i >= 0; i--) {
   GamePiece p = gamePieces[i];
   Log.d("GAME", "I will load " + p.getImage());
   Picasso.with(context).load(p.getImage()).into(target);
}
//loading the first one
Picasso.with(context).load(piece.getImage()).into(target);

And my target object is the next one:

Target target = new Target() {
       @Override
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
           Log.d("GAME", "Image loaded" + ++test);
           gameImage.setImageBitmap(bitmap); //ImageView to show the images
       }

       @Override
       public void onBitmapFailed(Drawable arg0) {}

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

I want to pre load the images so i can show one by one in the ImageView any time the user clicks a button.

The first image is loading so fast(that's cool) but the other images at the for loop never get load. How can i fix this? i need the images to start loading in the for loop.

Upvotes: 8

Views: 10349

Answers (2)

Fernando Santiago
Fernando Santiago

Reputation: 2288

I had to use: Picasso.with(getActivity().getApplicationContext()).load(p.getImage()).fetch();

Here is the reference: https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html

Upvotes: 5

Joel
Joel

Reputation: 838

maybe you can try doing the following:

Picasso mPicasso = Picasso.with(context); //Single instance

//if you are indeed loading the first one this should be in top, before the iteration.
Picasso.with(context).load(piece.getImage()).into(target);

Log.d("GAME", "Loading all images");
for (int i = gamePieces.length-1; i >= 0; i--) {

   GamePiece p = gamePieces[i];
   Log.d("GAME", "I will load " + p.getImage());
   mPicasso.load(p.getImage()).into(target); 

}

You can alway refer to the examples here

Upvotes: 0

Related Questions