简体   繁体   中英

Bitmap image from file does not scale on high density device

I have problem with displaying bitmap image on imageview on high density screen (480x800). When the bitmap image loaded from file on sdcard, the image does not scale to fit hdpi screen. On medium density screen it works normal (320x480).

public static Bitmap getBitmapFromFile(String src) {
    File file = new File(src);

    if (file.exists()) {
        return BitmapFactory.decodeFile(src);
    }

    return null; 
} 

mImage.setImageBitmap(Util.getBitmapFromFile(filePath));

Screenshoot on hdpi & mdpi http://202.148.2.34/~lorenz/iview.jpg

Try adding a scale type to it

mImage.setScaleType(ScaleType.FIT_XY);

There are lot of scaling type available. Check which one suits you.

This is an old question but I think there is an easier way to do this. It seems that when you load a bitmap from an external source (one not in your /res folder) the Android framework will treat it as if it was meant for the baseline device (ie mdpi). Therefore it looks great on a 320x480 mdpi screen, but not as much on an hdpi screen.

Now the system normally should scale for this on an hdpi screen by a factor of 1.5 (unless you supposedly you set a density in the bitmap object, but I don't really know how that works) but since you used the method setImageBitmap() the bitmap is not scaled, leading to your problem.

Supposedly if you use the method setImageDrawable() and wrap the bitmap in a Drawable the bitmap will be autoscaled just like you wanted.

I haven't tried this, but apparently this guy had the opposite problem of you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM