我想在Android中获取位图的尺寸而不读取整个文件。
我尝试使用推荐的inJustDecodeBounds
和记录InputStream
的自定义read()
。不幸的是,基于此,Android BitmapFactory
似乎会读取大量字节。
类似于: Java/ImageIO getting image dimensions without reading the entire file?适用于Android,不使用ImageIO。
答案 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;