【问题标题】:Picasso not loading a particular image url毕加索没有加载特定的图片网址
【发布时间】:2015-09-18 17:36:21
【问题描述】:

我正在尝试使用 picasso 加载此 http://assets.myntassets.com/v1/images/style/properties/Jockey-Men-Black-Innerwear-Vest-9926-0105_435418e1b17c4d0c5583dd33ba24193d_images.jpg 网址。检查日志时显示errored。但是当我通过以下方式尝试相同的网址时:

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

图片正在加载。这很奇怪。不知道为什么会这样。

这里是日志:

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)

毕加索的代码:

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

对于这种特殊情况,heightwidth 分别是 7201184

【问题讨论】:

  • 请显示一些代码。
  • @FaroukTouzi:添加代码。
  • 您可以尝试加载到Target,而不是直接加载到ImageView。这样您就可以轻松调试正在发生的事情。 square.github.io/picasso/javadoc/com/squareup/picasso/…
  • 尝试从 url 中删除 - 只需使用 _
  • 还要检查错误尝试将监听器绑定到毕加索

标签: android android-image picasso


【解决方案1】:

Here 是 github 上报告的此问题的链接。

【讨论】:

    【解决方案2】:

    按照此示例,您将获得适合 MAX_WIDTH 和 MAX_HEIGHT 范围内的图像(保持纵横比)

    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);
    

    这是我自定义的 BitmapTransform 类:

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

    /** * 转换加载的图像以避免 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;
    }
    
    };
    

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2015-11-14
      • 2015-11-16
      • 1970-01-01
      相关资源
      最近更新 更多