I have a method which I am making to save an image and show its progress while saving. Overall the method seems to work, but if I call it more than once it sometimes gives me a IllegalArgumentException error.
This is the stack trace:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.getImageReaders(ImageIO.java:641)
at com.forseth11.ColorEncoder.Encoder.saveImage(Encoder.java:308)
at com.forseth11.ColorEncoder.Encoder.encode(Encoder.java:82)
at com.forseth11.ColorEncoder.Encoder$3.run(Encoder.java:376)
at java.lang.Thread.run(Thread.java:745)
I don't know why I get this error or how to fix it. I need to be able to call this method multiple times to save multiple images in a row.
Here is the method:
private void saveImage(final BufferedImage image, final String string, final File outputfile) throws IOException, InterruptedException {
Thread thread = new Thread(new Runnable(){
public void run() {
try {
ImageIO.write(image, string, outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
try {
ImageInputStream iis = ImageIO.createImageInputStream(outputfile);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(iis);
try {
BufferedImage img = reader.read(0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();//This is the line the error is pointing to.
try {
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("png");
if (writers.hasNext()) {
ImageWriter writer = writers.next();
writer.addIIOWriteProgressListener(new IIOWriteProgressListener() {
public void imageStarted(ImageWriter source, int imageIndex) {
}
public void imageProgress(ImageWriter source, float percentageDone) {
System.out.println("UPDATE: " + percentageDone);//TODO
}
public void imageComplete(ImageWriter source) {
}
public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) {
}
public void thumbnailProgress(ImageWriter source, float percentageDone) {
}
public void thumbnailComplete(ImageWriter source) {
}
public void writeAborted(ImageWriter source) {
}
});
writer.setOutput(ios);
try {
writer.write(img);
} finally {
writer.removeAllIIOWriteProgressListeners();
}
}
} finally {
reader.removeAllIIOReadProgressListeners();
}
} finally {
reader.removeAllIIOReadProgressListeners();
}
}
} catch (IOException exp) {
exp.printStackTrace();
}
thread.join();//I added this so it will not leave the method until the image is saved.
}
I got the code for getting the percentage the image is saved from here. I modified it a bit.
How can I make this method to save an image and print its percentage saved without getting an error? Also why does it not always give me an error when I call the method two times in a row?