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
and here's the 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.
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.
My guess is you should not call that fit()
method in Picasso while your ImageView
has its width and height defined by 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.
Do not forget add permission to manifest
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
If you are using networkPolicy(NetworkPolicy.OFFLINE) then it might possible that you will not get images using Picasso.
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.
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 here and 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.
来源:https://stackoverflow.com/questions/25114045/picasso-not-loading-the-image