Android. Obtaining image size from it's resource id

前端 未结 2 1492
滥情空心
滥情空心 2020-12-15 04:15

This is a part of my Activity:

private ImageView mImageView;
private int resource;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.on         


        
相关标签:
2条回答
  • 2020-12-15 05:05

    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
    
    0 讨论(0)
  • 2020-12-15 05:06

    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;
    
    0 讨论(0)
提交回复
热议问题