获取位图的宽度和高度,而无需装载到内存位图、宽度、高度、内存

2023-09-06 13:35:23 作者:爱,是恒久忍耐!

林在Android的一个新手,所以我想知道有没有什么办法让一个位图的尺寸,而无需加载位图到内存中。??

Im a newbie in android, So i would like to know is there any way to get the dimensions of a Bitmap without loading the bitmap into memory.??

推荐答案

您可以设置inJustDe codeBounds的BitmapFactory.Options获取图像的宽度和高度,而无需装载在内存中的位图像素

You can set the BitmapFactory.Options with inJustDecodeBounds to get the image width and height without loading the bitmap pixel in memory

BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, bitmapOptions);
int imageWidth = bitmapOptions.outWidth;
int imageHeight = bitmapOptions.outHeight;
inputStream.close();

有关详细信息:

公共布​​尔inJustDe codeBounds

public boolean inJustDecodeBounds

自:API级别1如果设置为true,   去codeR将返回null(没有位图),但出...字段将   仍然被设置,使得主叫用户查询位图,而不必   分配内存为它的像素。

Since: API Level 1 If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels.

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inJustDe$c$cBounds