在Android中从Picasso下载多个图像

时间:2014-07-18 09:50:40

标签: android download picasso

我使用Picasso库从URL下载图片。这是我对毕加索的第一次尝试

场景:我想从服务器下载一些图像并将它们存储到一个文件中。我知道如何存储到文件和检索。当我运行下面的代码时,我碰巧看到我只获得了最后一张图片。看起来Picasso并行运行。我通过显示一个Toast消息来检查它。无论如何要解决这个问题吗?

问题:我只获取最后一个网址图片。

这是我的代码

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

6 个答案:

答案 0 :(得分:4)

请尝试这种方式,希望这有助于您解决问题。

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

答案 1 :(得分:4)

这是完美的答案,我已经对它进行了测试,并且像魅力一样。 您需要使用专用线程从网络中检索多个图像。

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

在主线程中的任何位置调用AsyncTask。

new loadImg().execute;

答案 2 :(得分:1)

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

getView()方法中使用此代码。

答案 3 :(得分:0)

不要以这种方式使用该方法而是拨打电话,并根据响应呼叫下一个电话。 像method1 - &gt; error(),result

处理两者的URL列表并调用相同的方法。

答案 4 :(得分:0)

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

在这种方法中,图像加载取决于您下载图像的互联网速度..

答案 5 :(得分:0)

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