我需要获取存储在我的Android设备上的图像的高度和宽度。我已经知道如何获取文件(图像),我只需要一些代码即可从该文件获取高度和宽度。
答案 0 :(得分:1)
当然可以。
您可以尝试以下操作:
Bitmap bitmap = BitmapFactory.decodeFile("your image path");
int width = bitmap.getWidth();
int height = bitmap.getHeight();
注意:您应该有权访问该文件。
答案 1 :(得分:0)
如果您只需要图像的尺寸,而不需要解码图像本身,则可以将BitmapFactory.Options
与inJustDecodeBounds = true
一起使用:
BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
decodeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, decodeOptions);
//decodeOptions.outWidth and decodeOptions.outHeight now contain the image dimensions
这样,解码函数将返回null,但设置options对象的out变量。这样可以节省CPU和内存,如果您一次要处理大量图像,这将是有利的。