0

Say I have a bitmap referenced in xml, eg res\drawable\reference.xml:

 <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
 android:src="@drawable/original"
 />

The first thing I tried was

BitmapFactory.Options opts= new BitmapFactory.Options();
opts.inJustDecodeBounds=true;
BitmapFactory.decodeResource(res, R.drawable.reference, opts);

The problem is now opts.outWidth and opts.outHeight == -1. This is because BitmapFactory doesn't know how to follow this alias to the original file - in fact it tries do decode the compiled xml file and fails.

This question has one answer to the problem but does not address the memory concerns:

It uses res.getDrawable(R.drawable.reference) to get a BitmapDrawable, and then get the Bitmap from that. This is not what I want. It is a very large image and I only want its dimensions.

Akshay
  • 2,506
  • 4
  • 34
  • 55
rockgecko
  • 4,127
  • 2
  • 18
  • 31
  • If this drawable is attached to a widget then why don't you just measure that widget ? – ADM Jan 24 '18 at 05:11
  • I'am curious, if `@drawable/original` is statically compiled in your app, why are you trying to get it's dimensions dynamically? – artkoenig Jan 24 '18 at 08:08
  • @ArtjomKönig the images are large. I want to determine how much memory they will need before decoding them, to avoid OutOfMemory error. I have several static background images and pick one at runtime at random. You're right, I guess I could create a file listing the dimensions of each image and ship that too, but I'm asking for a better way – rockgecko Jan 24 '18 at 11:24
  • @ADM it is not attached to a widget, it is a background image, and the image is usually larger than the screen – rockgecko Jan 24 '18 at 11:25
  • If you are not doing image processing . Then you should use a Image loader to load Bitmaps to avoid outofmemory error . – ADM Jan 25 '18 at 01:03

0 Answers0