Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading multiples images from Picasso in Android

I'm using Picasso library to download images from URL. This is my first attempt on Picasso

Scenario : I want to download some images from server and store them into a file. I know how to store into file and retrieve. When I ran the below code, I happen to see that I'm getting only last image. It seems like Picasso runs parallelly. I checked it by displaying a toast message. Is there anyway to solve this issue?

Problem : I'm getting only the last url image.

Here's my code

static int  count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    for (int i = 0; i < url.length; i++)
    {

        // url is String array which has 2 urls. 
        ++count;   // Incrementing the count by 1
        Picasso.with(this).load(url[i])
        .into(new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
                // TODO Auto-generated method stub
                arg0 = Bitmap.createScaledBitmap(arg0, 150, 150, true);
                filePath = saveFile(arg0);   // I'm just calling this function to check how many times `onBitmapLoaded` is called. And it is called only once...!!
            }

            @Override
            public void onBitmapFailed(Drawable arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
}

public String saveFile (Bitmap bm)
{

   Toast.makeText(getApplicationContext(), ""+count, 100).show(); // Displaying the value of count, which always display as **2**. 
   return "";
}
like image 648
Aniruddha Avatar asked Jul 18 '14 09:07

Aniruddha


People also ask

How do you download multiple pictures on android?

Hold down Shift and click further down in the photo roll if you want to select multiple photos at once. Click on the three dots at the top right and click Download (or use Shift + D). Your download should begin.


1 Answers

Try this way,hope this will help you to solve your problem.

static int  count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    for (int i = 0; i < url.length; i++)
    {

        // url is String array which has 2 urls. 
        Picasso.with(this).load(url[i])
        .into(new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
                // TODO Auto-generated method stub
                arg0 = Bitmap.createScaledBitmap(arg0, 150, 150, true);
                ++count; // Incrementing the count by 1
                filePath = saveFile(arg0);   // I'm just calling this function to check how many times `onBitmapLoaded` is called. And it is called only once...!!
            }

            @Override
            public void onBitmapFailed(Drawable arg0) {
                // TODO Auto-generated method stub

            }
        });
    }
}

public String saveFile (Bitmap bm)
{

   Toast.makeText(getApplicationContext(), ""+count, 100).show(); // Displaying the value of count, which always display as **2**. 
   return "";
}
like image 94
Haresh Chhelana Avatar answered Sep 18 '22 05:09

Haresh Chhelana