我的保存屏幕截图的类工作正常:
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;
}
}
问题是,当我在写保护区域执行程序时。程序只是关闭。我宁愿它只是不保存屏幕截图。如何做到这一点?
最佳答案
为什么不直接使用 try/catch?
try{
PixmapIO.writePNG(fh, pixmap);
} catch (Exception e) {
// save it somewhere else
}
pixmap.dispose();
关于java - LibGDX - 将屏幕截图保存在写保护文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20933718/