【问题标题】:Downloading multiples images from Picasso in Android在 Android 中从毕加索下载多张图片
【发布时间】:2018-10-31 14:26:28
【问题描述】:

我正在使用 Picasso 库从 URL 下载图像。这是我第一次尝试毕加索

场景:我想从服务器下载一些图像并将它们存储到一个文件中。我知道如何存储到文件和检索。当我运行下面的代码时,我碰巧看到我只得到最后一张图片。毕加索似乎并行运行。我通过显示吐司消息来检查它。有没有办法解决这个问题?

问题:我只得到最后一个 url 图片。

这是我的代码

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

【问题讨论】:

  • 是的,当您在时间 count 加载的第一个图像位图在迭代中达到值 2 时,它总是显示 count 2 becz。
  • 你说得对。谢谢@Haresh。所以我可以用这段代码将许多图像保存到文件中?
  • 我发布了答案,现在您可以检查并更清楚地了解它。
  • ,你看看我的回答了吗?

标签: android download picasso


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

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

【讨论】:

  • 您应该使用专用线程并使用.get(),而不是.into()
【解决方案2】:

这是完美的答案,我已经对其进行了测试,并且效果很好。 您需要使用专用线程从网络中检索多个图像。

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;

【讨论】:

    【解决方案3】:
    Picasso.with(MainActivity.this).load(Uri.parse(card.getField("profile_picture_url"))).into(viewHolder.tvPersence);
    

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

    【讨论】:

      【解决方案4】:

      不要以这种方式使用该方法,而是进行调用并根据响应调用下一次调用。 像method1 -> error(),结果

      处理它们的url列表并调用相同的方法。

      【讨论】:

      • 对不起,我没有得到你。你能解释一下吗?还是伪代码?
      【解决方案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);
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-11-16
          • 1970-01-01
          • 1970-01-01
          • 2015-08-24
          • 2017-07-14
          • 2014-04-27
          • 2015-11-14
          相关资源
          最近更新 更多