我保存屏幕截图的课程运行正常:
public class ScreenshotSaver {
private static int counter = 1;
public static void saveScreenshot() {
FileHandle fh;
do {
fh = new FileHandle("screenshot" + counter++ + ".png");
} while (fh.exists());
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
PixmapIO.writePNG(fh, pixmap);
pixmap.dispose();
}
private static Pixmap getScreenshot(int x, int y, int w, int h, boolean flipY) {
Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888);
ByteBuffer pixels = pixmap.getPixels();
Gdx.gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);
final int numBytes = w * h * 4;
byte[] lines = new byte[numBytes];
if (flipY) {
pixels.clear();
pixels.get(lines);
} else {
final int numBytesPerLine = w * 4;
for (int i = 0; i < h; i++) {
pixels.position((h - i - 1) * numBytesPerLine);
pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
}
pixels.clear();
pixels.put(lines);
}
return pixmap;
}
}
问题是,当我在写保护区执行程序时。程序只是关闭。我更喜欢它只是没有保存屏幕截图。怎么做?
答案 0 :(得分:3)
为什么不使用try / catch?
try{
PixmapIO.writePNG(fh, pixmap);
} catch (Exception e) {
// save it somewhere else
}
pixmap.dispose();
答案 1 :(得分:0)
对于运行时文件编写,使用Gdx.files.local(...)
。请参阅libgdx wiki中的file handling。它可以在任何地方工作,但在浏览器中使用GWT /。