Android的 - 宽度和位图的高度,而无需加载它 [英] Android - width and height of bitmap without loading it
问题描述
我需要得到一个位图的宽度和高度,但使用这给了内存不足的异常:
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 = 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();
我读的问题<一个解决方案href=\"http://stackoverflow.com/questions/11681274/get-bitmap-width-and-height-without-loading-to-memory\">Get位图的宽度和高度,而无需加载到内存中但你会在这里InputStream的?
I read the solution at the question Get bitmap width and height without loading to memory but what would be the inputStream here?
推荐答案
您需要指定一些 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
将不包含任何位字节数组。 采取>的inJustDe codeBounds属性设置为true,而解码避免了内存分配,返回null位图对象,但设置outWidth,outHeight和outMimeType。这种技术允许你之前的位图的构造(以及存储器分配)读出的图像数据的尺寸和类型。
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.
这篇关于Android的 - 宽度和位图的高度,而无需加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!