BitmapDrawable with unexpected size in Android 2.0.1 WVGA854

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" />

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

Thank you in advance.

Asked by: Carina730 | Posted: 20-01-2022

Answer 1

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?

Answered by: Lenny407 | Posted: 21-02-2022

Similar questions

Android - How to change grey background created by a BitmapDrawable

I'm using the follow code, to create a non-scaled, centred image as a background, in a relative layout:- RelativeLayout explosionlayout = (RelativeLayout) findViewById (; explosionlayout.setBackgroundColor(R.color.white); Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb); BitmapDrawable test1 = new BitmapDrawable(myBitmap); test1.setGravity(Gr...

android - Saving a BitmapDrawable or Bitmap

Okay, so I have a code, that take an image of the current view, and turns it into a bitmap, then I end up here, Bitmap bm = view.getDrawingCache(); BitmapDrawable bitmapDrawable = new BitmapDrawable(bm); now, what I'm trying to do, is take a picture of what the current view looks like, but from here, I can easily put bitmapDrawable into an ImageView, but thats not what I want,...

image - Android BitmapDrawable Constructor Undefined

I'm trying to load an image from the web. The code I have so far is as follows: Resources res = getResources(); InputStream is = (InputStream) new URL(url).getContent(); BitmapDrawable bitmapDrawable = new BitmapDrawable(res, is); // Error: The constructor BitmapDrawable(Resources, InputStream) is undefined The last line produces an error as if the constructor doesn't exist. However, the docum...

android - How to change size of BitmapDrawable in accordance with size of TextView?

I have TextView with background picture in my layout and would like to apply an animation to the text only. So, as I was advised in the separate question: you could try embedding a TextView and a BitmapDrawable in a FrameLayout, then apply the animation to ...

android - Adjust BitmapDrawable or bitmap dimensions

I am trying to create a tab-like interface where I will switch views by sliding left/right. I display to the user what page they are on by adding a blue glow to the back of the tab icon shown here: I did this by creating a custom view that will draw the background glow (if the tab is selected) and then the icon. My issue is that the glow i...

android - Creating device density independent BitmapDrawable from file

I'm getting a Bitmap from file (cached from web) and short of hard coding the device density I can't get it to draw at the correct size. I'm using the image as a menu icon so I can't set the scale type as far as I'm aware. I've tried this and got nowhere: BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inScaled = true; //tried false too opt.inDensity = 240; opt.inTargetDensity =...

android - Setting alpha on a BitmapDrawable in XML

I was wondering if its possible to set transparency in XML for a Drawable. In this case its setting the disabled state of a button. Instead of using a separate image i should like to create a grayed-out version by changing the alpha. I know I could do that in code. But can it also be predefined in XML?

How to reduce the memory of bitmapdrawable in Android 4.0?

I have an image existed in device sdcard. The path is /mnt/sdcard/001.jpg Here is my activity public class BitmapMemoryTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imv = (ImageView)findViewById(

android - BitmapDrawable deprecated alternative

I have the following code that will rotate a drawable by a set amount of degrees. public Drawable rotateDrawable(float angle, Context context) { Bitmap arrowBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.generic2rb); // Create blank bitmap of equal size Bitmap canvasBitmap = arrowBitmap.copy(Bitmap.Config.ARGB_8888, true); canvasBitmap.eraseColor(0x...

Android BitmapDrawable setTileModeX does not work on TextView

I have a TextView and a bitmap that can be repeated only horizontally. I want to set the background of my textview and repeat it only on the X axis. After looking around I saw that you can only do that via code and not in XML. I created a BitmapDrawable using:, BitmapDrawable bg = new BitmapDrawable(r, BitmapFactory.decodeResource(r, R.drawable.my_drawable)); bg.setTileModeX(Shader...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android