Daniel Benedykt
Daniel Benedykt

Reputation: 6553

android drawable changes sizes on screen when reading image from file

I have an image on a private file. I read the file, create the drawable, and assign it to an ImageView. The ImageView has WRAP_CONTENT so the size is automatic.

On 320x480 screens, the image looks good But on screens with more resolution and high density 480x800 or 480x854 (N1, droid) , when the image is for example 150x150, I see the image as 100x100.

Of course it has something to do with the density but not sure how should I resolve this.

This is my code:

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "icon");

fis.close();

imageView.setImageDrawable(icon);

thanks

================================================================

update:

with the following code:

FileInputStream fis = this.openFileInput("icon.png");

icon = Drawable.createFromStream(fis, "icon");

if I then inspect the size of the icon, android thinks the size is 100x100, when really is 150x150.

Looks like its reducing the image by the density. Can anybody explain this and how to avoid this.

Thanks

Upvotes: 6

Views: 6333

Answers (4)

Try BitmapDrawable#setTargetDensity:

Bitmap b = BitmapFactory.decodeFile(path)
BitmapDrawable bmd = new BitmapDrawable(b);
bmd.setTargetDensity(getResources().getDisplayMetrics());

Upvotes: 2

Steven Yelton
Steven Yelton

Reputation: 241

I am working on a similar issue, try this:

TypedValue typedValue = new TypedValue();
//density none divides by the density, density default keeps the original size
typedValue.density = TypedValue.DENSITY_DEFAULT;
Drawable d = Drawable.createFromResourceStream(null, typedValue, fis, "icon");

I am still working through how to develop a general solution to pick resolution/resize images from a generic stream instead of the drawable-* directories.

Upvotes: 3

Habib
Habib

Reputation: 21

Rescale the image to desired dimension:

            d = Drawable.createFromPath(filePath);
            if (d != null) {
                Bitmap bitmapOrg = ((BitmapDrawable) d).getBitmap();
                int width = bitmapOrg.getWidth();
                int height = bitmapOrg.getHeight();
                int newWidth = 170;
                int newHeight = 170;
                // calculate the scale
                float scaleWidth = ((float) newWidth) / width;
                float scaleHeight = ((float) newHeight) / height;
                // create a matrix for the manipulation
                Matrix matrix = new Matrix();
                // resize the bit map
                matrix.postScale(scaleWidth, scaleHeight);
                Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                width, height, matrix, true);
                // make a Drawable from Bitmap to allow to set the BitMap
                d = new BitmapDrawable(resizedBitmap);
            }

Upvotes: 2

Jim Blackler
Jim Blackler

Reputation: 23169

Set some dimensions in device independent pixels (or DIPs).

Upvotes: 1

Related Questions