0

It appears that BitmapFactory.decodeResource() is ignoring inScaled when inJustDecodeBounds is also "true".

In this snip of code:

Resources res = getResources ();
DisplayMetrics metrics = res.getDisplayMetrics ();

BitmapFactory.Options options = new BitmapFactory.Options ();
options.inScaled = true;
options.inDensity = DisplayMetrics.DENSITY_HIGH; // resolution of bitmap in resources
options.inTargetDensity = metrics.densityDpi; // screen resolution

options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeResource (res, srcId, options);
logD ("IVH decodeBitmapResource.1: " + options.outWidth + "," + options.outHeight + " - " + srcId);

options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeResource (getResources(), srcId, options);
logD ("IVH decodeBitmapResource.2: " + options.outWidth + "," + options.outHeight + " - " + srcId);

I get the following output in my log file:

IVH decodeBitmapResource.1: 420,747 - 2130837613
IVH decodeBitmapResource.2: 840,1494 - 2130837613

All I did was change inJustDecodeBounds from true to false. What am I missing?

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

1 Answers1

1

Perhaps the answer should be obvious: inJustDecodeBounds is supposed to ignore all other directives, even scaling.

The following code, when setting inJustDecodeBounds to "true" solves the problem:

float scale = (float)metrics.densityDpi / (float)DisplayMetrics.DENSITY_HIGH;

imageWidth = (int) (options.outWidth * scale + 0.5);
imageHeight = (int) (options.outHeight * scale + 0.5);

That said, come on Google. You've got all the code in this API; why are you making me rewrite it. Just provide a different flag that does everything but render the bitmap. Then, with one flag, I could compute bounds and, by flipping the flag, render a bitmap to exactly fit.

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • You say "supposed to" but I don't see it in the javadoc. So it's either a bug in the docs or a bug in the platform, but we don't know which one. – Doug Stevenson Feb 22 '16 at 22:07
  • Ok, let me put on my "sarcastic" hat. In my opinion, a lot of Google's Android documentation leaves plenty of semantic details to your imagination. – Peri Hartman Feb 23 '16 at 04:54
  • You're right. That's why they let you file bugs: https://code.google.com/p/android/issues/entry?template=Developer%20Documentation – Doug Stevenson Feb 23 '16 at 04:59