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.
Related
I calculate inSampleSize to use Bitmap decode method to resize large png files.
When creating a this new bitmap and Log info it's width and height with .getWidth() and .getHeight(), its pixels count has grown by 3 relative to its original size.
code: from line 121-204
https://github.com/abisai1221/android-bitmaps/blob/master/png%20to%20bmp
all help is greatly appreciated.
I would bet on screen density being applied. When you load bitmaps from resources, you have to put them in right drawable folders, because Android will automatically scale them to match your phone's screen density. Each folder should contain only images for one specific density. For example, if you have a high density screen in your phone, you should put images in drawable-hdpi folder to get them without scaling. If you're getting 3 times larger images, you probably have a very dense screen, like Nexus 5 or recent Galaxy S phone. To disable scaling, put images in drawable-nodpi folder.
I saw another post of resizing issues and came across the ".inScaled" field of BitmapFactory.options.
This executes by default to scale bitmap to target density.
I set this field to false to prevent it from executing and the images come out perfectly, tho Im still confused as to why it scales the image by a factor of 3.
I have created a dress up game app. After the user finishes dressing up his character, he/she can save it to the app's gallery.
I am using canvas and drawbitmap on canvas to generate the final image, which is then shown on the app's gallery. The xhdpi image size should be 600x900 pixels. When this image is shown in the normal android gallery, it is shown as 600x900 and it (got from info) looks big enough. However, when I show it in my app, it looks much smaller though the ImageView is set to wrap_content. Moreover, when dressing it up, it is also set to wrap_content and it looks bigger than the one in the app's gallery.
My theory is that because I'm trying to set it programmatically using
.setImageDrawable(Drawable.createFromPath(path));
and because the screen is xhdpi, it is dividing the size by two.
What can I do?
Likely, you're testing on a device that is not xhdpi, but hdpi or mdpi.
When you provide a graphic in a density bucket, Android will take care of resizing that image so that it remains the same physical dimension across all devices (that is to say, size in cm on the screen).
If you would like to use the raw drawable, you can provide the graphic in the res/drawable-nodpi folder.
If you would like to retain the density buckets, but your graphic is too small, provide the correct sizes in res/drawable-mdpi, res/drawable-hdpi and res/drawable-xhdpi.
I don't believe there is any way of preventing this behavior through Drawable.createFromPath(). You could load the bitmap directly using BitmapFactory and provide it into the ImageView using setImageBitmap():
File f = new File(path);
Bitmap b = BitmapFactory.decodeFile(f.getAbsolutePath());
imageView.setImageBitmap(b);
When you get a bitmap from a resource for example with:
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.yourimage);
It is automatically scaled depending on the device's screen density. How can I avoid that and retrieve it in its original pixel size whatever the screen density is?
Thanks
What do you mean "It is automatically scaled"? When you call this method Android takes image from folder res/drawable-?dpi - depending on device where app is running. If your ldpi folder contains 30*30px image, mdpi contains 40*40px image and hdpi contains 60*60px, you will get bitmap one of this sizes(depends on device). Android does not change anything.
If you see that on the screen your image scaled somehow, check properties of ImageView.
1st. i'am new to Android coding :)
What I do is I load an Bitmap from my res/drawable-mdpi with
BitmapFactory.decodeResource(getResources(), R.drawable.cat_ground_01);
after I Log out the width/height of the Bitmap it tells me an other value then the Bitmap realy is.
That's kinda difficult to place Bitmaps pixelperfect e.g. overlap a bitmap of an face with an bitmap of mouth.
maybe I'am just missing some knowledge for this Topic :9
I hope you can help.
When you do Bitmapfactory.decodeResource(), Android by default will choose the "matched" dpi version to decode, what happen in your mentioned code will yields:
You can't specify whether it is in mdpi, hdpi or whatever, it will choose the version that match your running System. i.e., if you are running on a mdpi device, it will decode the mdpi version; in ldpi, then the ldpi version.
Suppose you are using a hdpi device, but no mdpi resource is defined, what it will do is take your mdpi resource, and during decode, it will make it into hdpi (i.e., it enlarge your mdpi bitmap to about 1.5x larger); again, if your device has lower resolution then it will shrink the image
I guess this is what happens to you. For BitmapFactory, it actually has the option to NOT scaling the image:
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
Set the inScaled to false will do.
Can't you just put the resource in the nodpi folder?
i.e. res/drawable-nodpi
This is what I've done in the past.
after I Log out the width/height of the Bitmap it tells me an other value then the Bitmap realy is.
Check your options.inTargetDensity and options.inDensity after loading the Bitmap from /drawable. They are not equal (160 and 240 for example). If options.inScaled set to true (default) - that's why the Bitmap is being automatically rescaled.
Another way is to use Bitmap.createScaledBitmap in order to rescale an image after loading. Because sometimes you need inScaled=true
//Target dimensions
int iW = 300;
int iH = 200;
Bitmap mOriginalBitmap = new Bitmap;
BitmapFactory.Options options = new BitmapFactory.Options();
//Load image from resource
mOriginalBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.sample_image_900px, options);
//Scale to target size
mOriginalBitmap = Bitmap.createScaledBitmap(mOriginalBitmap, iW, iH, true);
I have a view that I am drawing to a bitmap, saving to disk and then putting in an ImageView via setImageURI. However, when the image is displayed in the ImageView it is not being shown at the correct size. It is about 1/3 smaller than it should be. I'm guessing that this is a density issue, but I can't figure out what's going wrong (my emulator is WVGA). Ideas anyone?
I ran into this issue today too. I ended up loading a bitmap instead.
Bitmap bit = BitmapFactory.decodeFile(image_path);
mImageView.setImageBitmap(bit);
Which displays correctly.
You're right
This is a density issue. To ensure that your image displays in the correct dimensions regardless of the density of the device you should consider using dip (density independent pixel) units.
Also, Android 1.5 does not support image density -- ie. it doesn't know how to distinguish between mdpi, hdpi, ldpi bitmaps. Android 1.6 and above does. You can use Bitmap.setDensity() or BitmapDrawable.setTargetDensity()
Finally -- you mention it is 1/3 small that it should be which is a good indication that the problem is related to density as mdpi density is 160dpi and hdpi is 240dpi -- 160/240 = 2/3 which is 1/3 less than your original image.
Hope this helps!
Try this:
ImageView photo = (ImageView)findViewById(R.id.photo);
photo.setScaleType(ImageView.ScaleType.CENTER);