0

I am trying to load this http://assets.myntassets.com/v1/images/style/properties/Jockey-Men-Black-Innerwear-Vest-9926-0105_435418e1b17c4d0c5583dd33ba24193d_images.jpg url using picasso. On checking the logs it's showing errored. But when I tried the same url by:

InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);

The image is loading. It is very strange. Could not figure why it is happening though.

Here is the logcat:

D/Picasso (27123): Main        created      [R9] Request{http://assets.myntassets.com/v1/images/style/properties/Jockey-Men-Black-Innerwear-Vest-9926-0105_435418e1b17c4d0c5583dd33ba24193d_images.jpg resize(720,1184) centerInside}
D/Picasso (27123): Dispatcher  enqueued     [R9]+7ms 
D/Picasso (27123): Main        created      [R10] Request{http://assets.myntassets.com/v1/images/style/properties/Jockey-Men-Black-Innerwear-Vest-9926-0105_435418e1b17c4d0c5583dd33ba24193d_images.jpg resize(720,1184) centerInside}
D/Picasso (27123): Hunter      executing    [R9]+15ms 
D/Picasso (27123): Hunter      removed      [R9]+21ms from 
D/Picasso (27123): Dispatcher  canceled     [R9]+21ms 
D/Picasso (27123): Dispatcher  enqueued     [R10]+16ms 
D/Picasso (27123): Hunter      executing    [R10]+16ms 
D/Picasso (27123): Dispatcher  retrying     [R10]+538ms 
D/Picasso (27123): Hunter      executing    [R10]+542ms 
D/Picasso (27123): Dispatcher  retrying     [R10]+1057ms 
D/Picasso (27123): Hunter      executing    [R10]+1062ms 
D/Picasso (27123): Dispatcher  batched      [R10]+1586ms for error (will replay)

Code for picasso:

Picasso.with(context)
                    .load(url)
                    .resize(height,width)
                    .centerInside()
                    .into(imgView);

Where height and width for this particular case is 720 and 1184 respectively.

Shubham
  • 3,071
  • 3
  • 29
  • 46
  • Show some code please. – Farouk Touzi Aug 27 '15 at 11:04
  • @FaroukTouzi: Added the code. – Shubham Aug 27 '15 at 11:16
  • You can try to load into a `Target` instead of directly into the `ImageView`. This way you can easily debug what is going on. https://square.github.io/picasso/javadoc/com/squareup/picasso/Target.html – lgvalle Aug 27 '15 at 11:18
  • try removing `-` from url simply use `_` – karan Aug 27 '15 at 11:19
  • also to check error try binding listener to picasso – karan Aug 27 '15 at 11:22
  • I found a way to get more data on the error. maybe this can help: http://stackoverflow.com/questions/27798391/android-picasso-error-reason/ – R. Adang Aug 27 '15 at 11:25
  • @KaranMer: I am afraid `onError()` of `ImageLoadedCallBack` does not have any parameter for why Image Failed to load. – Shubham Aug 27 '15 at 11:26
  • you can use Picasso picasso = new Picasso.Builder(Profile.this) .listener(new Picasso.Listener() { @Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { System.out.println("Ex"+exception.toString()); } }) .build(); picasso.load(profilephoto) .transform(new CircleTransform()) .into(img); – karan Aug 27 '15 at 11:26
  • Okay Here is the issue: `https://github.com/square/picasso/issues/364` – Shubham Aug 27 '15 at 11:53

2 Answers2

1

Here is the link to this issue reported on github.

Shubham
  • 3,071
  • 3
  • 29
  • 46
0

Following this example you will get a image that fits inside MAX_WIDTH and MAX_HEIGHT bounds (keeping aspect ratio)

private static final int MAX_WIDTH = 1024;
private static final int MAX_HEIGHT = 768;

int size = (int) Math.ceil(Math.sqrt(MAX_WIDTH * MAX_HEIGHT));

// Loads given image
Picasso.with(imageView.getContext())
   .load(imagePath)
   .transform(new BitmapTransform(MAX_WIDTH, MAX_HEIGHT))
   .skipMemoryCache()
   .resize(size, size)
   .centerInside()
   .into(imageView);

And this is my custom BitmapTransform class:

import android.graphics.Bitmap;
import com.squareup.picasso.Transformation;

/** * Transformate the loaded image to avoid OutOfMemoryException */

public class BitmapTransform implements Transformation {

    int maxWidth;
    int maxHeight;

    public BitmapTransform(int maxWidth, int maxHeight) {
        this.maxWidth = maxWidth;
        this.maxHeight = maxHeight;
    }

 @Override
 public Bitmap transform(Bitmap source) {
    int targetWidth, targetHeight;
    double aspectRatio;

    if (source.getWidth() > source.getHeight()) {
        targetWidth = maxWidth;
        aspectRatio = (double) source.getHeight() / (double)     source.getWidth();
        targetHeight = (int) (targetWidth * aspectRatio);
    } else {
        targetHeight = maxHeight;
        aspectRatio = (double) source.getWidth() / (double)   source.getHeight();
        targetWidth = (int) (targetHeight * aspectRatio);
    }

    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth,  targetHeight, false);
    if (result != source) {
        source.recycle();
    }
    return result;
}

@Override
public String key() {
    return maxWidth + "x" + maxHeight;
}

};
Nirmal Shethwala
  • 268
  • 1
  • 15