在Android中获取位图的尺寸而不读取整个文件

时间:2012-08-18 12:36:25

标签: android

我想在Android中获取位图的尺寸而不读取整个文件。

我尝试使用推荐的inJustDecodeBounds和记录InputStream的自定义read()。不幸的是,基于此,Android BitmapFactory似乎会读取大量字节。

类似于: Java/ImageIO getting image dimensions without reading the entire file?适用于Android,不使用ImageIO。

2 个答案:

答案 0 :(得分:13)

你是正确使用inJustDecodeBounds。将其设置为true并确保在解码中包含选项。这只是读取图像大小。

无需读取流。只需在解码中指定路径即可。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(<pathName>, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;

答案 1 :(得分:0)

java.net.URL imageUrl = new java.net.URL("https://yourUrl.com");
HttpURLConnection connection = (HttpURLConnection)imageUrl.openConnection();
connection.connect();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(connection.getInputStream(), null, options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
相关问题