Benni
Benni

Reputation: 969

Libgdx Screenshot method not working

A couple of weeks ago I implemented this method https://github.com/libgdx/libgdx/wiki/Take-a-Screenshot
And it worked great with libgdx 1.3.1 . Now though I upgraded to 1.6.0 and it have stopped working. When the method is executed it freezes. I have it implemented on a button, and it gets stuck in "downclick" and nothing more happens.

private void saveScreenshot() {
    try{
        FileHandle fh;
        do{
            fh = new FileHandle(files.getLocalStoragePath() + "screenshot" + ".png");

        }while(fh.exists());

        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() - 130, true);
        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
        System.out.println("Path:" + fh.toString());

    }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);
    w = pixmap.getWidth();
    h = pixmap.getHeight();
    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;
}

 btnArrow.addListener(new ChangeListener() {
        //photoshop "save" and "back" on arrow/back image to clarify.
        @Override
        public void changed(ChangeEvent event, Actor actor) {
            saveScreenshot();
            sharePhoto();

        }
    });

I share the image to facebook aswell. And this method is in AndroidLauncher of course and is passed through an interface. And here I fetch the screenshot:

public void sharePhoto() {
    Matrix matrix = new Matrix();

    String filePath = (files.getLocalStoragePath() + "screenshot" + ".png");
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    Bitmap rotateBit = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);


    //Starts sharing process
    SharePhoto photo = new SharePhoto.Builder()
            .setBitmap(rotateBit)
            .build();
    SharePhotoContent content = new SharePhotoContent.Builder()
            .addPhoto(photo)
            .build();
    share.show(content);

}

So what I believe may be the issue is libgdx have done changes on Pixmap class or Bitmap class of some sort. Since sharing a link through facebook on that button works fine.

I also printed the path as you can see in saveScreenshot() and it returns this

selinux_android_setcategory: no category for userid: 0, path: /data/data/com.sparc.tormt.android/lib

Upvotes: 1

Views: 203

Answers (1)

P.T.
P.T.

Reputation: 25177

Is it stuck because this is an infinite loop if the file already exists:

do {
   fh = new FileHandle(files.getLocalStoragePath() + "screenshot" + ".png");
} while(fh.exists());

Upvotes: 2

Related Questions