获取位图宽度和高度而不加载到内存

Ren*_*ith 15 android bitmap

我是Android的新手,所以我想知道是否有任何方法可以获得位图的尺寸而无需将位图加载到内存中.

Han*_*non 30

您可以使用inJustDecodeBounds设置BitmapFactory.Options以获取图像宽度和高度,而无需在内存中加载位图像素

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

更多细节:

public boolean inJustDecodeBounds

从以下版本开始:API Level 1如果设置为true,解码器将返回null(无位图),但out ...字段仍将设置,允许调用者查询位图而无需为其像素分配内存.

http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inJustDecodeBounds