Downloading multiples images from Picasso in Android

一笑奈何 提交于 2019-12-09 06:17:41

问题


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

回答1:


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



回答2:


This is perfect answer and I've tested it and works like a charm. You need to use a dedicated thread to retrieve multiple images from network.

private class loadImg extends AsyncTask<Void,Integer,Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                for(int i=0;i<mAdapter.getItemCount();i++){
                    Object object = mAdapter.getItem(i);
                    final String link = object.getImageLink();//"YOUR IMAGE LINK OR STRING";
                    if(link!=null && !link.isEmpty()) {
                        Bitmap bitmap = Picasso.with(context).load(link).get();
                        publishProgress(i);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            mAdapter.notifyItemChanged(values[0]);
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
        }

Call your AsyncTask any where in the Main thread.

new loadImg().execute;



回答3:


Picasso.with(MainActivity.this).load(Uri.parse(card.getField("profile_picture_url"))).into(viewHolder.tvPersence);

use this code in your getView() method.




回答4:


Don't use the method in this way rather make a call and depending upon the response call the next call . Like method1 -> error(), result

Handle the list of url at both of them and call the same method.




回答5:


    final ImageView iv = new ImageView(context);

    Picasso.with(context).load(resultImageUrl).into(iv, 
       new Callback() {
           @Override
           public void onSuccess() {
              // Picasso successfully loaded image from resultImageUrl

              Bitmap bitmap = ((BitmapDrawable) 
              iv.getDrawable()).getBitmap();

              ImageStorage.saveToSdCard(context, bitmap, imageName);

              onFinishDownloading(results);
           }

           @Override
           public void onError() {
              // Picasso has failed to load image from resultImageUrl

              onFinishDownloading(results);
           }
    });



回答6:


Picasso.with(context).load(url).into(imageview);

in this approach image loading is depends on the speed of your internet to download the image..



来源:https://stackoverflow.com/questions/24822086/downloading-multiples-images-from-picasso-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!