An image pulled from resources will get up-scaled if one cannot be found for the target device's density dpi. For example, if you are on a device that is DisplayMetrics.DENSITY_HIGH
(hdpi) but you only have the image in /res/drawable-mdpi
that image will be automatically up-scaled when you retrieve it via something like getDrawable()
.
However, with a downloaded image the system does not know which density the image is designed for because it is not contained in a resource folder which specifies its density, so scaling cannot be done automatically. You must define the density manually using BitmapFactory.Options
. Consider the following function:
/**
* Downloads an image for a specified density DPI.
* @param context the current application context
* @param url the url of the image to download
* @param imgDensity the density DPI the image is designed for (DisplayMetrics.DENSITY_MEDIUM, DisplayMetrics.DENSITY_HIGH, etc.)
* @return the downloaded image as a Bitmap
*/
public static Bitmap loadBitmap(Context context, String url, int imgDensity) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
BitmapFactory.Options options = new BitmapFactory.Options();
// This defines the density DPI the image is designed for.
options.inDensity = imgDensity;
// These define the density DPI you would like the image to be scaled to (if necessary).
options.inScreenDensity = metrics.densityDpi;
options.inTargetDensity = metrics.densityDpi;
try {
// Download image
InputStream is = new java.net.URL(url).openStream();
return BitmapFactory.decodeStream(is, null, options);
}
catch(Exception ex) {
// Handle error
}
return null;
}
So if your image at the specified URL is designed for an mdpi screen, you would pass DisplayMetrics.DENSITY_MEDIUM
for the imgDensity
parameter. If the current context has a larger density DPI (such as DisplayMetrics.DENSITY_HIGH
), the image will be up-scaled accordingly.