2015-08-27 3 views
0

Я пытаюсь загрузить этот код http://assets.myntassets.com/v1/images/style/properties/Jockey-Men-Black-Innerwear-Vest-9926-0105_435418e1b17c4d0c5583dd33ba24193d_images.jpg, используя пикассо. При проверке журналов отображается errored. Но когда я попробовал тот же URL-адрес:Picasso не заказывая определенное изображение url

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

Изображение загружается. Это очень странно. Не могу понять, почему это происходит.

Вот 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) 

Код для Picasso:

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

Где height и width для данного конкретного случая является 720 и 1184 соответственно.

+0

Показать код пожалуйста. –

+0

@FaroukTouzi: Добавлен код. – Shubham

+0

Вы можете попробовать загрузиться в «Целевое», а не непосредственно в «ImageView». Таким образом, вы можете легко отладить, что происходит. https://square.github.io/picasso/javadoc/com/squareup/picasso/Target.html – lgvalle

ответ

0

После этого примера вы получите изображение, которое легко помещается в 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; 
} 

}; 
Смежные вопросы