I used Picasso and had the same problem. image was too large at least in on size, width or height. finally I found the solution here. you can scale the large image down according to display size and also keep the aspect ratio:
public Point getDisplaySize(Display display) { Point size = new Point(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { display.getSize(size); } else { int width = display.getWidth(); int height = display.getHeight(); size = new Point(width, height); } return size; }
and use this method for loading image by Picasso:
final Point displySize = getDisplaySize(getWindowManager().getDefaultDisplay()); final int size = (int) Math.ceil(Math.sqrt(displySize.x * displySize.y)); Picasso.with(this) .load(urlSource) .resize(size, size) .centerInside() .into(imageViewd);
also for better performance you can download the image according to width and height of the display screen, not whole the image:
public String reviseImageUrl(final Integer displayWidth, final Integer displayHeight, final String originalImageUrl) { final String revisedImageUrl; if (displayWidth == null && displayHeight == null) { revisedImageUrl = originalImageUrl; } else { final Uri.Builder uriBuilder = Uri.parse(originalImageUrl).buildUpon(); if (displayWidth != null && displayWidth > 0) { uriBuilder.appendQueryParameter(QUERY_KEY_DISPLAY_WIDTH, String.valueOf(displayWidth)); } if (displayHeight != null && displayHeight > 0) { uriBuilder.appendQueryParameter(QUERY_KEY_DISPLAY_HEIGHT, String.valueOf(displayHeight)); } revisedImageUrl = uriBuilder.toString(); } return revisedImageUrl; } final String newImageUlr = reviseImageUrl(displySize.x, displySize.y, urlSource);
and then:
Picasso.with(this) .load(newImageUlr) .resize(size, size) .centerInside() .into(imageViewd);
EDIT: getDisplaySize()
display.getWidth()/getHeight() is deprecated. Instead of Display use DisplayMetrics.
public Point getDisplaySize(DisplayMetrics displayMetrics) { int width = displayMetrics.widthPixels; int height = displayMetrics.heightPixels; return new Point(width, height); }
inSampleSizeapproach.