【问题标题】:Picasso not loading the image毕加索没有加载图像
【发布时间】:2014-09-26 15:40:37
【问题描述】:

这是毕加索的代码:

ImageView img = (ImageView)findViewById(R.id.product_image);
    Picasso.with(this)
    .load(_url)
    .fit()
    .into(img, new Callback() {

        @Override
        public void onSuccess() {


        }

        @Override
        public void onError() {


        }
    });

这是 _url 的值: http://kiagallery.ir/Images/Upload/Collection%20101/Spring%20101/d4a03b66dc7b46c694615c549b78b2e9.jpg

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView 
    android:id="@+id/product_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView 
    android:id="@+id/product_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="13sp"
    android:layout_below="@id/product_image"/>

<TextView
    android:id="@+id/product_price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="13sp"
    android:layout_below="@id/product_name"/>
</RelativeLayout>

如您所见,可以通过浏览器访问该图像,但 picasso 无法加载它,我检查了 onError 函数,它从未被调用,我在这里很安静,任何帮助将不胜感激。

编辑:当我给 imageview 的宽度和高度固定值(如 200dp)时,它会加载图像,但当我将其更改为 wrap_content 时,它不会显示图像。

【问题讨论】:

    标签: android imageview picasso


    【解决方案1】:

    我遇到了类似的问题,我将 ViewPager 的图像放在全屏 DialogFragment 内。相同的 ViewPager 在放置在其他地方时工作得很好,毕加索按预期加载了图像。但只有放在全屏DialogFragment 时,一些图像才会显示为空白。似乎已成功从服务器获取图像并加载到ImageView,但ImageView 仍然显示为空白。

    以上答案或类似主题都不适合我。

    所以我切换到另一个名为 Glide 的优秀库。它与毕加索非常相似。 了解更多 herehere。 尽管这可能不是最相关的答案,但对于遇到类似问题的人来说可能值得一试。

    【讨论】:

      【解决方案2】:

      我猜你应该在 Picasso 中调用 fit() 方法,而你的 ImageView 的宽度和高度由 WRAP_CONTENT 定义。

      此方法等到ImageView 被测量并调整图像大小以完全匹配它的大小。虽然您的 ImageView 的大小由 WRAP_CONTENT 定义,但方法 getMeasuredWidth()getMeasuredHeight() 似乎返回 0,这使您的 ImageView 不可见

      【讨论】:

      • 是的,正如github.com/square/picasso/issues/457 中提到的那样...所以我将删除 fit() 调用,并将图像的宽度固定为设备的 90%,这应该可以解决它
      • 我有完全相同的代码,但它仍然没有加载它。我也将宽度和高度更改为 200dp 并添加了清单文件的权限,可能会出现什么问题?
      【解决方案3】:

      如果您使用的是 networkPolicy(NetworkPolicy.OFFLINE),那么您可能无法使用 Picasso 获取图像。

      【讨论】:

        【解决方案4】:

        不要忘记为清单添加权限

        <manifest ...>
            <uses-permission android:name="android.permission.INTERNET" /> 
        

        【讨论】:

        • 天哪!我也错过了添加这个!
        猜你喜欢
        • 2016-07-25
        • 2021-08-09
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-29
        相关资源
        最近更新 更多