android:获取图像尺寸而不打开它

sto*_*fln 45 android

我希望在将它们加载到RAM之前获得存储在SD卡上的图像的宽度和高度(以像素为单位).我需要知道尺寸,所以我可以在加载时相应地对它们进行下采样.如果没有对它们进行下采样,我会得到一个OutOfMemoryException.

任何人都知道如何获取图像文件的尺寸?

Dev*_*red 117

传递选项只是解码工厂的边界:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//If you want, the MIME type will also be decoded (if possible)
String type = options.outMimeType;
Run Code Online (Sandbox Code Playgroud)

HTH

  • 高度和宽度返回0 (2认同)