Android - 位图的宽度和高度,不加载它

时间:2013-07-24 10:51:52

标签: android bitmap

我需要获取位图的宽度和高度,但使用它会产生内存不足的异常:

    Resources res=getResources();
    Bitmap mBitmap = BitmapFactory.decodeResource(res, R.drawable.pic); 
    BitmapDrawable bDrawable = new BitmapDrawable(res, mBitmap);

    //get the size of the image and  the screen
    int bitmapWidth = bDrawable.getIntrinsicWidth();
    int bitmapHeight = bDrawable.getIntrinsicHeight();

我在问题Get bitmap width and height without loading to memory上阅读了解决方案,但这里的inputStream是什么?

2 个答案:

答案 0 :(得分:16)

您还需要指定一些BitmapFactory.Options

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不包含任何位图字节数组。取自hereSetting 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.

答案 1 :(得分:6)

使用此

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

请参阅http://developer.android.com/training/displaying-bitmaps/load-bitmap.html