保存截图并在Facebook上分享



我希望用户点击一个按钮,它会截图屏幕,然后开始在Facebook中共享过程。

这就是我拍摄和处理屏幕截图的方式:

private void saveScreenshot() {
    try{
        FileHandle fh;
        do{
            fh = new FileHandle(Gdx.files.getLocalStoragePath() + "stoneIMG" + counter++ + ".png");
        }while(fh.exists());
        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
        System.out.println(fh.toString());
        Gdx.app.exit();
    } catch(Exception e) {
    }
}
private Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
    final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
    if(yDown) {
        ByteBuffer pixels = pixmap.getPixels();
        int numBytes = w * h * 4;
        byte[] lines = new byte[numBytes];
        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;
}

然后:

SharePhoto photo = new SharePhoto.Builder()
    .setBitmap(image);
    .build();
SharePhotoContent content = new SharePhotoContent.Builder()
    .addPhoto(photo)
    .build();

分享一下。但我不知道如何将这两者联系起来。我的意思是,屏幕截图将PNG图像保存到一些存储器中,但我不知道在哪里。有人有主意吗?

首先,您需要截图:

saveScreenshot();

文件路径为Gdx.files.getLocalStoragePath() + "stoneIMG" + counter + ".png"(查看FileHandle对象)。要将文件加载到Bitmap,可以使用以下代码:

String filePath = Gdx.files.getLocalStoragePath() + "stoneIMG" + counter + ".png";
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

然后您可以使用发送

SharePhoto photo = new SharePhoto.Builder()
    .setBitmap(bitmap);
    .build();
SharePhotoContent content = new SharePhotoContent.Builder()
    .addPhoto(photo)
    .build();

最新更新