I want to save some java.awt.Image objects into the disk. But when I try like:
for (Image image : images) {
image = new ImageIcon(image).getImage();
BufferedImage temp = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = temp.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
File tempFile = null;
try {
tempFile = File.createTempFile("img", ".jpg");
ImageIO.write(temp, "jpg", tempFile);
} catch (IOException ex) {
ex.printStackTrace();
}
files.add(tempFile);
}
I get java heap space error.
I needed to put
image = new ImageIcon(image).getImage();
to get all the pixels loaded so that image.getwidth(null) and image.getHeight(null) return valid results.
Is there any other way to do this except for increasing heap size?