Reputation: 1208
Is there any way in Java to take image width and height without transfer or download?
mentions a way using ImageIO, but ImageIO is not available on Android.
Android has BitmapFactory, which can decode the image to get the width and height without loading the entire image to memory, but it only works with files.
So the question is on Android, is there a way to get just the image width and height from an web url without downloading the entire image?
Upvotes: 0
Views: 2293
Reputation: 762
It is possible in Java, using ImageReader.getHeight(int) and ImageReader.getWidth(int) normally only reads the image header so image is not downloaded entirely, not sure it will work in Android:
public static Dimension getImageDimensionFromUrl(final URL url)
throws IOException {
try (ImageInputStream in = ImageIO.createImageInputStream(url.openStream())) {
final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
if (readers.hasNext()) {
final ImageReader reader = readers.next();
try {
reader.setInput(in);
return new Dimension(reader.getWidth(0), reader.getHeight(0));
} finally {
reader.dispose();
}
}
}
return null;
}
Upvotes: 1
Reputation: 3212
It's not possible without downloading the image. BitmapFactory
cannot be applied unless and until the image is downloaded.
Upvotes: 0