android:无需打开即可获取图像尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11128481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android: get image dimensions without opening it
提问by stoefln
I want to get width and height (in pixels) of images which are stored on the sdcard, before loading them into RAM. I need to know the size, so I can downsample them accordingly when loading them. Without downsampling them I get an OutOfMemoryException.
我想在将它们加载到 RAM 之前获取存储在 SD 卡上的图像的宽度和高度(以像素为单位)。我需要知道大小,因此我可以在加载它们时相应地对它们进行下采样。没有对它们进行下采样,我会得到一个 OutOfMemoryException。
Anyone knows how to get dimensions of image files?
任何人都知道如何获取图像文件的尺寸?
回答by Devunwired
Pass the option to just decode the bounds to the factory:
将选项传递给工厂只解码边界:
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;
回答by Tinh Duong
Actually, there is another way to solve this problem. Using the below way, we can avoid the trouble with file and URI.
实际上,还有另一种方法可以解决这个问题。使用以下方式,我们可以避免文件和 URI 的麻烦。
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);