Reputation: 71
I'm trying to save an image using Image.IO.Write()
; I have basically copied the standard code from here to take screenshots with lwjgl. The only thing I did was initialize the file with an existing directory as the save path.
When I try to save the image the FileNotFoundException
gets thrown.
glReadBuffer(GL_FRONT);
int width = Display.getDisplayMode().getWidth();
int height = Display.getDisplayMode().getHeight();
int bpp = 4; // Assuming a 32-bit display with a byte each for red, green, blue, and alpha.
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
File file = new File("res/screenshots/ss_" + Sys.getTime() + ".png"); // The file to save to.
String format = "PNG"; // Example: "PNG" or "JPG"
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
{
int i = (x + (width * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
try {
ImageIO.write(image, format, file);
} catch (IOException e) { e.printStackTrace(); }
stacktrace:
java.io.FileNotFoundException: res\screenshots\ss_91733792.png (The system cannot find the path specified)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at javax.imageio.stream.FileImageOutputStream.<init>(Unknown Source)
at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(Unknown Source)
at javax.imageio.ImageIO.createImageOutputStream(Unknown Source)
at javax.imageio.ImageIO.write(Unknown Source)
at com.va.util.Extras.takeScreenShot(Extras.java:46)
at com.va.core.Engine.start(Engine.java:117)
at Main.main(Main.java:26)
Exception in thread "main" java.lang.NullPointerException
at javax.imageio.ImageIO.write(Unknown Source)
at com.va.util.Extras.takeScreenShot(Extras.java:46)
at com.va.core.Engine.start(Engine.java:117)
at Main.main(Main.java:26)
Upvotes: 3
Views: 5692
Reputation: 121649
I assume "/res/screenshots" exists and is writeable, correct?
I'm guessing your program's current directory might be different than what you're expecting.
For debug purposes, please add these two lines (or equivalent):
String currentDir = new File(".").getAbsolutePath();
System.out.println ("Current directory: " + currentDir);
If "res/screenshots" is NOT under "currentDirectory", then change the relative path in your code.
// EXAMPLE:
File file = new File("../..res/screenshots/ss_" + Sys.getTime() + ".png");
Upvotes: 3
Reputation: 15699
The code you linked to, does not show how the file is created and leaves it to the programmer.
What you are doing is probably only creating a new File
object with
File img = new File("path/to/file.jpg");
Your problem is, the path/to
folder does not exist, and ImageIO
throws FileNotFoundException
.
to make this work, use
img.getParent().mkdirs();
before you pass the img
to ImageIO
. This way you make sure, that the full path to the file is accessible. As stated in the comments to this post, the file itself does not have to exist, but the path to id does.
Upvotes: 1