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, and then in your button press event, you’ll need something like this:
ScreenshotSaver ss = new ScreenshotSaver();
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:
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();
    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);
        MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
    } catch (FileNotFoundException e) {
    } catch (Exception e) {
    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 {
			} catch (IOException e) {
				// TODO Auto-generated catch block

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 ( == ApplicationType.Android)


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!