【问题标题】:Picasso cannot load images for some URL (no special characters)Picasso 无法为某些 URL 加载图像(无特殊字符)
【发布时间】:2015-02-10 07:42:48
【问题描述】:

我正在使用 Picasso 将一些在线图像加载到 listView 中。问题是,虽然有些图像已成功加载,但有些则只是消失了。

成功(品牌形象展示成功):

失败(未显示品牌形象,失败):

ImageView 失败时会消失。这是我的代码:

Picasso.with(mContext)
.load(UrlEncoder.encode(interiorDesign.getBrand_image_url()))
.config(Bitmap.Config.RGB_565)
.error(R.drawable.blank)
.fit()
.centerInside()
.into(holder.brand);

这是我的 .xml 文件:

<LinearLayout
android:layout_width="match_parent"
        android:layout_height="90dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"
        android:orientation="horizontal">

<RelativeLayout
android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingRight="10dp">
...
</RelativeLayout>

<ImageView
android:layout_width="200dp"
        android:layout_height="90dp"
        android:paddingBottom="10dp"
        android:id="@+id/partial_interior_design_brand" />
</LinearLayout>

我检查了它是否失败,因为它在 Picasso 的 error() 方法中捕获了一个错误。

Here 是失败的链接。

Here 是另一个失败的链接。

Here 是一个成功的链接。

我多次遇到这个问题。而且我怀疑问题出在 fit() 和 centerInside() 方法上,因为在我删除这两种方法后,问题就解决了。然而,如果没有这两种方法,我的图像根本不适合大小。

【问题讨论】:

  • 只用这个 Picasso.with(mContext).load(interiorDesign.getBrand_image_url()).config(Bitmap.Config.RGB_565) .error(R.drawable.blank) .fit() .centerInside () .into(holder.brand);
  • 我测试过。它不起作用。我切换到通用图像加载器,问题终于解决了。我不知道毕加索有什么问题。我经常遇到这种问题。
  • 好吧,你切换到了更好的库 :)
  • 我想我会在我的下一个项目中尝试 UIL。但是对于这个项目,由于它处于最后阶段,我担心其他隐藏的错误,我现在不想动手。还有其他建议吗?
  • @Derekyy 你能提供我没有显示的图片的网址吗?

标签: android listview picasso


【解决方案1】:

嘿,尝试将您的网址连接到“http://”。由于某种原因,picassa 不会在没有 http:// 的情况下加载图像。 因此,请尝试下一件事

Picasso.with(mContext)
.load("http://".concatenate(url))
.config(Bitmap.Config.RGB_565)
.error(R.drawable.blank)
.fit()
.centerInside()
.into(holder.brand);

【讨论】:

    【解决方案2】:

    我以这种方式使用毕加索,它总是加载我的图像:

    if (imageURL != null) {
        Picasso.with(getContext()).load(imageURL).error(R.drawable.ic_missing)
        .into(ivThumbnail);
    

    我的布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/itemview_campaign_background"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp" >
    
        <ImageView
            android:id="@+id/ivThumbnail"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    
    ... // More stuff
    

    【讨论】:

      【解决方案3】:

      除了切换到通用图像加载器之外,我找到了另一个解决方案,通过使用毕加索中的 Transform() 方法,并将位图的 targetHeight 指定为 ImageView 的高度。由于CenterInside 只是意味着位图必须完全位于imageView 内部,因此我还要检查targetWidth(缩放后)是否大于imageView 的宽度。如果是这样,我使用 targetWidth 作为参考点,而不是 targetHeight。

                  Picasso.with(mContext)
                          .load(url)
                          .transform(new Transformation() {
                              @Override
                              public Bitmap transform(Bitmap source) {
                                  int targetHeight = dpToPx(height_dp);
                                  double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                                  int targetWidth = (int) (targetHeight / aspectRatio);
                                  if (targetWidth > dpToPx(width_dp)) {
                                      targetWidth = dpToPx(width_dp);
                                      targetHeight = (int) (targetWidth * aspectRatio);
                                  }
                                  Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                                  if (result != source) {
                                      // Same bitmap is returned if sizes are the same
                                      source.recycle();
                                  }
                                  return result;
                              }
      
                              @Override
                              public String key() {
                                  return "transformation" + " desiredWidth";
                              }
                          })
                          .into(imageView);
      
      private int dpToPx(int dp) {
          float density = Resources.getSystem().getDisplayMetrics().density;
          return Math.round((float) dp * density);
      }
      

      编辑: 经过测试,我发现这种方法在某些情况下是行不通的。还在想办法。

      【讨论】:

        【解决方案4】:

        我和你有同样的问题,

        有些图片我可以正确加载,其他的不是,比如:

        它们是相同的,但网址名称不同。

        您最好上传到第三方上传网站并加载指向您的应用程序的直接链接以避免您的问题。

        【讨论】:

          【解决方案5】:

          无需使用 UrlEncoder

          .load(UrlEncoder.encode(interiorDesign.getBrand_image_url()))

          只是 URL 的字符串

          .load(interiorDesign.getBrand_image_url())
          

          【讨论】:

            猜你喜欢
            • 2019-08-20
            • 1970-01-01
            • 1970-01-01
            • 2018-06-02
            • 2020-08-03
            • 1970-01-01
            • 2015-09-06
            • 2019-08-14
            • 2017-01-16
            相关资源
            最近更新 更多