Android - width and height of bitmap without loading it

后端 未结 2 1512
渐次进展
渐次进展 2021-01-04 04:41

I need to get the width and height of a bitmap but using this gives an out of memory exception:

    Resources res=getResources();
    Bitmap mBitmap = Bitmap         


        
相关标签:
2条回答
  • 2021-01-04 04:43

    You need to specify some BitmapFactory.Options as well:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    

    bDrawable will not contain any bitmap byte array. Taken from here: Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null for the bitmap object but setting outWidth, outHeight and outMimeType. This technique allows you to read the dimensions and type of the image data prior to construction (and memory allocation) of the bitmap.

    0 讨论(0)
  • 2021-01-04 05:02

    Use this

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    

    see http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    0 讨论(0)
提交回复
热议问题