Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. Obtaining image size from it's resource id

This is a part of my Activity:

private ImageView mImageView;
private int resource;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  resource = getIntent().getIntExtra("res", -1);

  Matrix initMatrix = new Matrix();

  mImageView = new ImageView(getApplicationContext());
  mImageView.setScaleType( ImageView.ScaleType.MATRIX );
  mImageView.setImageMatrix( initMatrix );
  mImageView.setBackgroundColor(0);
  mImageView.setImageResource(resource);
}

I try to display an image within an ImageView using a matrix as scale type (I want to add multitouch later). But before user starts interaction i want the image to be centered and fit inside the ImageView. I already found answers concerning how to solve it but there is one problem for me: to make image centered using matrix I need to know its width and height. Is there any way of getting image size when all you have is int resource ?

like image 564
alex Avatar asked Mar 11 '12 13:03

alex


People also ask

How do I find the image ID of a resource?

I'll use getResources(). getIdentifier(name, "id", getPackageName()); to get the ID of an ImageButton (as you would with R.id.name). @Srujan Barai, getResource() and getPackageName() are methods from Activity.

What is the use of resource ID in android?

When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.

What is the use of set image resource in Android?

setImageResource(): Use a resource id to set the content of the ImageView. setImageUri(): Use a URI to set the content of the ImageView.

What is the use of set image resource method?

If you don't want to set image to xml file and show image programmatically then you need to use this method. For E.g. You set image in xml which showing ON Switch now when you click ON Button its should be set to OFF Switch so in that case you can set using this setImageResource() programmatically.


2 Answers

Use BitmapFactory.decodeResource to obtain a Bitmap object of the resource, and then from the bitmap you can easily retrieve the image width/height with getHeight and getWidth

Also do not forget to recycle your bitmap

EDIT:

This way you will get a null bitmap as output, but the BitmapFactory.Options will be set with the with and height for the bitmap. So, in this case,, you do not need to recycle the bitmap

BitmapFactory.Options dimensions = new BitmapFactory.Options(); 
dimensions.inJustDecodeBounds = true;
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap, dimensions);
int height = dimensions.outHeight;
int width =  dimensions.outWidth;
like image 52
Blackbelt Avatar answered Oct 18 '22 20:10

Blackbelt


For anyone that didn't read dmon's comment. The code to do this looks like this:

final Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.your_photo, opt);

opt.outHeight; // height of resource
opt.outWidth; // width of resource
like image 22
xbakesx Avatar answered Oct 18 '22 21:10

xbakesx