Screenshot and Share

Hi fairly new here I was hoping I could get some help with an app I’ve just released.

I am a new developer so to be honest coding is still fresh territory for me, I’ve just released my sixth app and it is doing fairly well (over 800 downloads in its first 24 hours) and I was wanting to add a feature where it takes a screenshot and allows you to share it as a post on facebook.

I’ve found a few code snippets, even an entire class on how to do it but none of these come with any documentation on how to implement them, I’ve been working at this for about 8 hours straight now and its 4AM and I am about to scratch my eyes out.

I was hoping that maybe some people here with a bit more experience than me could possibly help me out or point me in the right direction.

I found this example code which I put into a separate class in my app but I can’t figure out how to call on it when a button is pressed elsewhere within the app in a different class. This is using libGDX, hope you guys can help out!

I’m not familiar with libGDX. But based on the linked code I’m guessing there is a static class “Gdx” which is how you access the framework functions. If this is the case, it should be simple. Just copy the code into ScreenshotSaver.java, and then in your button press event, you’ll need something like this:
[JAVA]
ScreenshotSaver ss = new ScreenshotSaver();
ss.saveScreenshot(“myfilename”);
[/JAVA]
This creates a new ScreenshotSaver object, and then calls the “saveScreenshot” method. So long as it can access Gdx.* and ScreenUtils.*, it should work as far as I can see. The most important bit of code is:
[JAVA]
ScreenUtils.getFrameBufferPixels(true);
[/JAVA]
which actually gets the pixels from the screen.

Can anyone else who’s used libGDX shed some light on this?

Here is how I do it in Anagram Hero:


protected File getScreenshot(View view) {
    View v = view.getRootView();
    Boolean oldCacheSetting = v.isDrawingCacheEnabled();
    v.setDrawingCacheEnabled(true);
    Bitmap b = v.getDrawingCache();             
    String extr = Environment.getExternalStorageDirectory().toString();
    File myPath = new File(extr, "AH_screenshot.jpg");
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(myPath);
        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    v.setDrawingCacheEnabled(oldCacheSetting);
    return myPath;
}

Yes, it is horrid… should probably have a finally block to close the file handle/etc… but meh

Thanks both for the replies tried your method Dave but It is still throwing back various errors, how would i implement your method mind? As said I am still fairly new so I don’t have the faintest idea where to put something like that into my code? And whether I would still keep the example code I posted.

What errors are you getting? I was able to use it with Dave’s instructions, only had to add a try/catch block. Here’s what I did to use it:


			ScreenshotSaver ss = new ScreenshotSaver();
			try {
				ss.saveScreenshot("screenshot");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

I thought maybe you have to call it from your render method (libgdx uses a render loop), but was able to call it from a keypress event no problem. Correct me if I’m wrong but the class your using is not going to work on android? If you look at line 37 of the saveScreenshot it has:

if (Gdx.app.getType() == ApplicationType.Android)

return;

So it’s going to stop if your using it on an android app. I only tested on the desktop, didn’t test on android. Maybe because java.awt and javax packages are not available on android so it stops to keep from having an error? I’ve saved screenshots with another class that saves as a .jpg (with libgdx in android). But the class I have is one I found about 10 yrs ago and I don’t know where to link it for you. PM if you want and I’ll email you the class.

Thanks for pointing out that android issue, personally I wouldn’t have spotted that and it would have driven me nuts! Will PM now!

Just want to take the time to thank XdebugX publicly, he spent quite a bit of his own free time helping me implement this feature into my app, absolute god send! Has pushed my daily installs from 1000 to 2000 and it’s still in its first week, logged on today to see its just made it into top free new releases! So again thanks a million!

Glad I could help!