用Picasso在Android上加载多张图片

3

我正在尝试使用Picasso库从多个URL加载图像。

到目前为止,我已经尝试了以下代码:

 for(int i = 0; i < friends.size(); i++)
   {
       final Profile profile = friends.get(i);
       String url = profile.getUserImageUrl();


       Picasso.with(getContext()).load(url).into(new Target() {
           // It doesn't reach any of the code below ....!! 

           @Override
           public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)    {
               profile.setUserImage(bitmap);
               counter++;

               if(counter >= friends.size() - 1)
                   cards();
           }

           @Override
           public void onBitmapFailed(Drawable drawable) {
               Log.e("App", "Failed to load company logo in onBitmapFailed method");
           }

           @Override
           public void onPrepareLoad(Drawable drawable) {
               Log.e("App","Failed to load company logo in onBitmapFailed method");
           }

       });
   }

这段代码不起作用。 当我运行这段代码时,它无法到达Target接口中的任何一行。有人对此有什么想法吗?

2个回答

1
当请求正在运行时,您只需要保留对“Target”的强引用。而且,每次要加载的图片都需要一个不同的“Target”实例(因为如果我没记错的话,Picasso将取消先前为同一“Target”启动的请求,如果有一个新的请求开始)。 解释: 实际上,您遇到此问题的原因是:
注意:此方法对“Target”实例保持弱引用,如果您没有对其进行强引用,则会被垃圾回收。要在图像加载时接收回调,请使用into(android.widget.ImageView、Callback)。
来源:http://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html#into-com.squareup.picasso.Target- 因此,通常情况下:
大多数情况下,当您处理自定义视图或视图持有者应该实现Target接口时,应使用此选项。

但是:

在您的情况下,我认为最好的解决方案就是事先创建/查找ImageViews,并让Picasso直接将图像加载到它们中。


0
也许我在这里漏掉了什么,但我认为into()只接受一个ImageView和一个可选的Callback。你能做到像这样吗:
Picasso
    .with(getContext())
    .load(profile.getUserImageUrl())
    .into(imageView, new Callback()
    {
        @Override
        public void onSuccess()
        {
            // Update card
        }

        @Override
        public void onError()
        {
            Log.e("App","Failed to load company logo");
        }
    });

但是让我们试试这个:我猜你要么是想将个人资料图片添加到一堆现有视图中,要么是在循环遍历所有个人资料时动态地尝试创建这些视图。以下是两种情况的解决方案:

for (int i = 0; i < friends.size(); i++)
{
    Profile profile = friends.get(i);
    if (profile != null)
    {
        /** Either find an existing view like this: **/

        // You're assembling a resource ID here.
        String resourceName = "profile_" + profile.getId(); // Assuming you have an ID.
        int resourceId = getResources().getIdentifier(resourceName, "id", getActivity().getPackageName());

        // Use it to get the image view in your activity's layout.
        ImageView imageView = (ImageView) findViewById(resourceId);
        if (imageView != null)
        {
            Picasso
                    .with(this)
                    .load(profile.getUserImageUrl())
                    .into(imageView);
        }

        /** Or inflate a View like this: **/

        // Get a containing view. You should move this above your
        // loop--it's here so I can keep these blocks together.
        FrameLayout frameLayout = (FrameLayout) findViewById(R.layout.frame_layout);

        // This is a layout that contains your image view and
        // any other views you want associated with a profile.
        View view = LayoutInflater.from(this).inflate(R.layout.profile_layout, null, false);

        // You're finding the view based from the inflated layout, not the activity layout
        ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
        if (imageView != null)
        {
            Picasso
                    .with(this)
                    .load(profile.getUserImageUrl())
                    .into(imageView);
        }

        frameLayout.addView(view);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接