3

I've got an if-else scenario where the if-path uses this code:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeResource(getResources(), resourceId, options);

And the else-path uses this code:

InputStream is = getContentResolver().openInputStream(Uri.fromFile(file));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(is, null, options);

Now to me, reading the short description from the API, testing and Googling I can't quite figure out the difference, but there seems to be a suble one.

The first piece of code scales to fit the width of the screen when applied to a ImageView (within a RelativeLayout, within a ScrollView). The second piece of code does not, which is my problem. It seems to loose it's aspect ratio somehow as well, because if I apply:

imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);

It manages to fit it to the width, but messes up the height (making it shorter than it should be). Any input would be greatly appreciated!

(I've already tried to change the first piece of code to see if I had messed something else up, but this rewrite of the if-path gave the same error:)

BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap= BitmapFactory.decodeStream(getResources().openRawResource(resourceId), null, options);
TragedyStruck
  • 586
  • 8
  • 24
  • Well, if your source image and imageview are different aspect ratios... won't using ScaleType.FIT_XY mess with the aspect ratio to make it fit? Have yo utried the other scale types, e.g. FIT_CENTER or FIT_INSIDE? – Faris M Apr 23 '12 at 01:23
  • I hadn't tried it, but did now and no luck. Also, that is why I've got imageView.setAdjustViewBounds(true) to make the ImageView adjust to the bitmaps-aspect ratio, but it doesn't when I'm using decodeStream. – TragedyStruck Apr 23 '12 at 01:44
  • 1
    I was able to solve this problem using this solution: http://stackoverflow.com/questions/4677269/how-to-stretch-three-images-across-the-screen-preserving-aspect-ratio – TragedyStruck Apr 28 '12 at 22:12

0 Answers0