Screenshot blank image

Anything libgdx related goes here!

Screenshot blank image

Postby izax » Sat Sep 20, 2014 9:10 am

I am trying to take a screenshot of my game using the method provided here.

However, when I try to open the PNG file I have saved using any gallery app, it is but a pure BLACK image that I get..

I have read some post on the internet reporting the same issue, but none seems to have provided the solution for me.

Can someone please give me some hints what could go wrong and cause the BLACK image?
izax
 
Posts: 4
Joined: Sat Sep 20, 2014 9:03 am

Re: Screenshot blank image

Postby izax » Sat Sep 20, 2014 9:37 am

Attach code that simulate my problem as below.

Code: Select all
package com.libgdx.demo;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.PixmapIO;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;

import java.nio.ByteBuffer;

public class Demo extends ApplicationAdapter{
    SpriteBatch batch;
    Texture img;
    boolean executed = false;

    @Override
    public void create(){
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");
    }

    @Override
    public void render(){
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img, 0, 0);
        batch.end();

        if(!executed){
            executed = true;
            Pixmap snapshot = snap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
            save(snapshot, Gdx.files.getLocalStoragePath() + "snapshot.png");
            snapshot.dispose();
        }
    }

    private void save(Pixmap pixmap, String path){
        try{
            FileHandle handle = new FileHandle(path);
            PixmapIO.writePNG(handle, pixmap);
        }
        catch(Exception e){}
    }

    private Pixmap snap(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;
    }
}
izax
 
Posts: 4
Joined: Sat Sep 20, 2014 9:03 am

Re: Screenshot blank image

Postby veljkoi » Sat Sep 20, 2014 6:53 pm

Maybe you can try to call your snap method before spriteBatch.end().

I have similar problems, and that fixed my issue.
I'm not sure if this is best approach, but it worked for me at the time.
veljkoi
 
Posts: 127
Joined: Tue Jun 17, 2014 7:30 am

Re: Screenshot blank image

Postby izax » Sun Sep 21, 2014 2:18 am

veljkoi wrote:Maybe you can try to call your snap method before spriteBatch.end().

I have similar problems, and that fixed my issue.
I'm not sure if this is best approach, but it worked for me at the time.


Thank you for the reply veljkoi, I have found the problem.

My problem is that I am building my game on Android and I happened to save the PNG file in the internal storage. Therefore, the real issue in my case is actually about the permission to read the file from other app. I didn't realize this until I tried to copy the file from internal to external storage, so its my own problem actually. :(

But I really appreciate your reply, not like the people I met on Stack Overflow who just down vote on me without explaining, one even accuse me of not familiar with Stack Overflow. :evil:
izax
 
Posts: 4
Joined: Sat Sep 20, 2014 9:03 am

Re: Screenshot blank image

Postby izax » Sun Sep 21, 2014 2:30 am

As a note, I think I will explain my problem here a bit.

It doesn't work when I store the screenshot in Android internal storage (as shown by the code below):
Code: Select all
save(snapshot, Gdx.files.getLocalStoragePath() + "snapshot.png");


It works when I shifted the location to external storage (as shown by the code below):
Code: Select all
save(snapshot, Gdx.files.getExternalStoragePath() + "snapshot.png");


Thus, the real issue is about Android permission, internal storage is PRIVATE by default whereas external storage is PUBLIC.

Anyone who wants to save the screenshot in the internal storage and still be able to share it should create a Content Provider.
izax
 
Posts: 4
Joined: Sat Sep 20, 2014 9:03 am


Return to Libgdx

Who is online

Users browsing this forum: Google [Bot] and 1 guest