使用Picasso加载多个图像-Android

用户名

我正在尝试使用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界面内的任何行。有人对为什么有任何想法?

迈克尔·德索托

也许我在这里错过了一些东西,但我认为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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法使用Picasso库在android中加载图像

来自分类Dev

Android:使用Picasso延迟在horizontalscrollview中加载图像

来自分类Dev

无法使用Picasso库在android中加载图像

来自分类Dev

无法在 Android recyclerview 中使用 Picasso 从 Firestore 加载图像

来自分类Dev

Android-使用Picasso加载图像而不将其存储在缓存中

来自分类Dev

使用Picasso android disck下载图像

来自分类Dev

Android无法使用Picasso从api获取图像

来自分类Dev

Android Square Picasso无法加载波斯字符图像网址

来自分类Dev

将多个图像加载到GridView Android

来自分类Dev

将多个图像加载到GridView Android

来自分类Dev

为什么要使用Android Picasso库下载图像?

来自分类Dev

在Android中使用Picasso时无法显示图像

来自分类Dev

如何将多个图像加载到android图像库?

来自分类Dev

如何将多个图像加载到android图像库?

来自分类Dev

滚动RecyclerView时,Android Picasso图像加载应用程序崩溃

来自分类Dev

如何使用android baseadapter加载图像?

来自分类Dev

在Android中使用Glide加载图像

来自分类Dev

离线时使用 SSL 加载 Picasso 图像

来自分类Dev

Android Studio picasso 表示无法解析“加载”

来自分类Dev

网络连接关闭时,适用于Android的Picasso库是否可以处理图像加载?

来自分类Dev

Android,无法在Picasso中清除图像缓存

来自分类Dev

Android Picasso不显示来自SDCARD的图像

来自分类Dev

如何在Android中使用Picasso库将背景图像设置为活动布局

来自分类Dev

Android ArrayAdapter使用通用图像加载库加载列表视图图像

来自分类Dev

Android ArrayAdapter使用通用图像加载库加载列表视图图像

来自分类Dev

使用Glide for Android,如何从资产和资源加载图像?

来自分类Dev

如何使用Android Glide加载图像指定重试次数?

来自分类Dev

使用DiskBasedCache时未加载Android Volley ListView图像

来自分类Dev

当使用asynctask加载图像时,Android listview中的行为异常

Related 相关文章

  1. 1

    无法使用Picasso库在android中加载图像

  2. 2

    Android:使用Picasso延迟在horizontalscrollview中加载图像

  3. 3

    无法使用Picasso库在android中加载图像

  4. 4

    无法在 Android recyclerview 中使用 Picasso 从 Firestore 加载图像

  5. 5

    Android-使用Picasso加载图像而不将其存储在缓存中

  6. 6

    使用Picasso android disck下载图像

  7. 7

    Android无法使用Picasso从api获取图像

  8. 8

    Android Square Picasso无法加载波斯字符图像网址

  9. 9

    将多个图像加载到GridView Android

  10. 10

    将多个图像加载到GridView Android

  11. 11

    为什么要使用Android Picasso库下载图像?

  12. 12

    在Android中使用Picasso时无法显示图像

  13. 13

    如何将多个图像加载到android图像库?

  14. 14

    如何将多个图像加载到android图像库?

  15. 15

    滚动RecyclerView时,Android Picasso图像加载应用程序崩溃

  16. 16

    如何使用android baseadapter加载图像?

  17. 17

    在Android中使用Glide加载图像

  18. 18

    离线时使用 SSL 加载 Picasso 图像

  19. 19

    Android Studio picasso 表示无法解析“加载”

  20. 20

    网络连接关闭时,适用于Android的Picasso库是否可以处理图像加载?

  21. 21

    Android,无法在Picasso中清除图像缓存

  22. 22

    Android Picasso不显示来自SDCARD的图像

  23. 23

    如何在Android中使用Picasso库将背景图像设置为活动布局

  24. 24

    Android ArrayAdapter使用通用图像加载库加载列表视图图像

  25. 25

    Android ArrayAdapter使用通用图像加载库加载列表视图图像

  26. 26

    使用Glide for Android,如何从资产和资源加载图像?

  27. 27

    如何使用Android Glide加载图像指定重试次数?

  28. 28

    使用DiskBasedCache时未加载Android Volley ListView图像

  29. 29

    当使用asynctask加载图像时,Android listview中的行为异常

热门标签

归档