毕加索无法加载某些网址的图片(没有特殊字符)

时间:2015-02-10 07:42:48

标签: android listview picasso

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

成功(品牌形象已成功显示):

enter image description here

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

enter image description here

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()方法,因为在我删除这两个方法之后,问题就解决了。然而,如果没有这两种方法,我的图像就不合适了。

5 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

除了切换到通用图像加载器之外,我还通过在Picasso中使用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);
}

编辑: 经过测试,我发现这种方法在某些情况下不起作用。仍在制定解决方案。

答案 3 :(得分:0)

我遇到了和你一样的问题,

我可以加载正确的一些图像,其他图像不是,如:

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

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

答案 4 :(得分:0)

无需使用UrlEncoder

  

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

只是URL的字符串

.load(interiorDesign.getBrand_image_url())