views:

549

answers:

1

I want to display a 480x320 JPEG file stored in the SD card in Android 2.0.1 with WVGA854 skin and 240 lcd density (Motorola Droid).

However, when I create a BitmapDrawable through Drawable.createFromPath(), the resulting BitmapDrawable has the following values:

mBitmapWidth = 320

mBitmapHeight = 213

mTargetDensity = 160

The manifest includes the following configuration:

<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="4" />
<supports-screens 
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
 anyDensity="true"
/>

Apparently Android is resizing the bitmap. What could be the cause of this behavior? How can I avoid it?

Thank you in advance.

+1  A: 

Drawable.createFromPath() is a convenience method that uses BitmapFactory in its implementation. It seems that since API level 4 Android automatically rescales all bitmap drawables and other resources.

To get the original size of the bitmap one would need to cast the drawable into a bitmap drawable and use BitmapDrawable.geBitmap().getWidth() and BitmapDrawable.geBitmap().getWidth().

An alternative may be to use BitmapFactory directly and turn the rescaling off with the BitmapFactory.Options.inScaled. This property is available only since API level 4, so if you want to target API level 3 devices, you might want to use reflection to check for its presence.

I still do not understand why such rescaling is needed for SD card images and how this benefits programmers in any way. Yes, a 320x213 area in 160 density becomes (approximately) a 480x320 area in 240 density, but wouldn't it make more sense if Drawable.createFromPath() returned a 480x320 drawable with 240 density to an application running in 240 density?

hgpc