Upload multiple images with Picasso in the background

I am trying to load an array of 20 URLs in the background using Picasso. So far I have the following 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 as follows:

 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 preload images so that I can show one by one in ImageView every time the user clicks a button.

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

+6
source share
2 answers

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

Here is the link: https://square.imtqy.com/picasso/javadoc/com/squareup/picasso/RequestCreator.html

+3
source

maybe you can try 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 always refer to examples here.

0
source

Source: https://habr.com/ru/post/984240/


All Articles