Android 毕加索没有加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25114045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Picasso not loading the image
提问by arash moeen
here's the code for picasso:
这是毕加索的代码:
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() {
}
});
here's the value of _url: http://kiagallery.ir/Images/Upload/Collection%20101/Spring%20101/d4a03b66dc7b46c694615c549b78b2e9.jpg
这是 _url 的值:http: //kiagallery.ir/Images/Upload/Collection%20101/Spring%20101/d4a03b66dc7b46c694615c549b78b2e9.jpg
and here's the xml:
这是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>
as you can see the image can be accessed via browser but picasso fails to load it, I've checked the onError function and it's never called, I'm quiet lost here, any help would be appreciated.
正如您所看到的,可以通过浏览器访问该图像,但 picasso 无法加载它,我检查了 onError 函数并且它从未被调用过,我在这里很安静,任何帮助将不胜感激。
EDIT:When I give imageview's width & height fixed value like 200dp, it loads the image, but when I change it to wrap_content it doesn't show the image.
编辑:当我给 imageview 的宽度和高度固定值(如 200dp)时,它会加载图像,但是当我将其更改为 wrap_content 时,它不会显示图像。
回答by Defuera
Do not forget add permission to manifest
不要忘记添加清单权限
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
回答by Lubo? Stará?ek
My guess is you should notcall that fit()
method in Picasso while your ImageView
has its width and height defined by WRAP_CONTENT
.
我的猜测是,你应该不叫那个fit()
,而你的方法毕加索ImageView
有其宽度和高度由定义WRAP_CONTENT
。
This method wait until the ImageView
has been measured and resize the image to exactly match it's size. While your ImageView
is having size defined by WRAP_CONTENT
, then methods getMeasuredWidth()
and getMeasuredHeight()
seemingly returns 0 which is making your ImageView
invisible.
此方法等待直到ImageView
已测量并调整图像大小以完全匹配它的大小。虽然您ImageView
的大小由WRAP_CONTENT
,然后方法getMeasuredWidth()
和getMeasuredHeight()
看似返回 0 这使您ImageView
不可见。
回答by Anand Savjani
If you are using networkPolicy(NetworkPolicy.OFFLINE) then it might possible that you will not get images using Picasso.
如果您使用的是 networkPolicy(NetworkPolicy.OFFLINE) 那么您可能无法使用 Picasso 获取图像。
回答by kds23
I was facing a similar issue where I had a ViewPager
of images placed inside a fullscreen DialogFragment
. The same ViewPager worked perfectly when placed elsewhere and Picasso loaded the images as expected. But only when placed in the fullscreen DialogFragment
, some images would appear blank. It seemed that images were successfully fetched from server and loaded into the ImageView
, but the ImageView
was still appearing blank.
我遇到了一个类似的问题,我ViewPager
在全屏中放置了一个图像DialogFragment
。同一个 ViewPager 放置在其他地方时效果很好,毕加索按预期加载了图像。但只有在全屏时DialogFragment
,某些图像才会显示为空白。似乎图像已成功从服务器获取并加载到 中ImageView
,但ImageView
仍然显示为空白。
None of the above answers or similar threads worked for me.
以上答案或类似的主题都不适合我。
So I switched to another great library called Glide. It is pretty much similar to Picasso. Find out more hereand here. Even though this might not be the most relevant answer, it might be worth giving a shot for someone who has been stuck with a similar problem.
所以我切换到另一个名为Glide 的很棒的库。它与毕加索非常相似。在此处和此处了解更多信息。尽管这可能不是最相关的答案,但对于遇到类似问题的人来说,这可能值得一试。