在将存储在SD卡上的图像加载到内存之前,我想获取图像的宽度和高度(以像素为单位)。我需要知道图像的大小,以便在加载时适当地降采样它们。如果不降采样,我会出现OutOfMemoryException。
有人知道如何获取图像文件的尺寸吗?
在将存储在SD卡上的图像加载到内存之前,我想获取图像的宽度和高度(以像素为单位)。我需要知道图像的大小,以便在加载时适当地降采样它们。如果不降采样,我会出现OutOfMemoryException。
有人知道如何获取图像文件的尺寸吗?
将选项传递给工厂,仅解码边界:
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;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
ParcelFileDescriptor fd = mContext.getContentResolver().openFileDescriptor(u, "r"); // u is your Uri
BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, options);