I am using Picasso to load some images from online into a listView. The problem is, while some of the images are successfully loaded, some just simply disappear.
Successful (The brand image is successfully displayed):
Fail (no brand image is shown, failed):
The ImageView disappears when it fails. Here is my code:
Picasso.with(mContext)
.load(UrlEncoder.encode(interiorDesign.getBrand_image_url()))
.config(Bitmap.Config.RGB_565)
.error(R.drawable.blank)
.fit()
.centerInside()
.into(holder.brand);
Here is my .xml file:
<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>
I have checked that it fails because it catches an error in Picasso's error() method.
Here is link of the failed one.
Here is another failed link.
Here is a successful link.
The problem occurs to me several times. And I suspect the problem lies with fit() and centerInside() methods, since after I remove those two methods, problem solved. Yet without those two methods, my images simply don't fit the size.
Hey try just concatanate your url to "http://". For some reason picassa wont load image without http://.
So just try with next thing
Picasso.with(mContext)
.load("http://".concatenate(url))
.config(Bitmap.Config.RGB_565)
.error(R.drawable.blank)
.fit()
.centerInside()
.into(holder.brand);
I'm using Picasso this way and it always load my images:
if (imageURL != null) {
Picasso.with(getContext()).load(imageURL).error(R.drawable.ic_missing)
.into(ivThumbnail);
My layout:
<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
Besides switching to Universal Image Loader, I found another solution for this, by using Transform() method in Picasso, and specify the targetHeight of the bitmap to the height of the ImageView. Since CenterInside simply means that the bitmap must be entirely inside the imageView, I also check if the targetWidth (after scaling) is larger than the width of the imageView. If so, I use the targetWidth as the reference point instead of 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);
}
Edit:
After testing, I found that this method does not work in some cases. Still figuring out the solution.
I had same problem like you,
some images I can load correct, the others is not, like :
They are same, but different url name.
It's better you upload to third-party uploading site and load direct link to your application to avoid your issue.
no need to use UrlEncoder
.load(UrlEncoder.encode(interiorDesign.getBrand_image_url()))
just the String of URL
.load(interiorDesign.getBrand_image_url())