Edward Lim
Edward Lim

Reputation: 833

How to share a ScreenShot in my LibGDX game?

Ok so as I have mentioned in the title, that is what I am trying to accomplish. I would think that this is something very easy to implement.

1.) Invoke somesort of screenshot method that would save the .png file to somesort of path.

2.) Get path and start an intent using the passing the path through and tada, done!

Heres what I have so far but im just not sure whats wrong here, when I click on share the "dialog menu" pops out but when I click, for instance, gmail, the image is empty? Any help would be greatly appreciated. :D

CLASS IN MY ANDROID FILES:

public class ActionResolverAndroid implements ActionResolver {
    Context context;

    public ActionResolverAndroid(Context context) {
        this.context = context;
    }

    @Override
    public void shareIntent() {
        String filePath = Gdx.files.external("shareIMG.png").path();
        System.out.println(filePath);

        String text = "Look at my awesome picture";
        Uri pictureUri = Uri.parse(filePath);
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_TEXT, text);
        shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
        shareIntent.setType("image/*");
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(shareIntent, "Share images..."));
    }
}

ATTEMPT AND MAKING IT WORK IN CORE:

buttonRate.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            button_clicked.play();
            game.screenShotFactory.saveScreenshot();
            game.actionResolver.shareIntent();
        }
    });

LASTLY, THE SCREENSHOT CLASS:

public class ScreenshotFactory {

    public static void saveScreenshot(){
        try
        {
            Gdx.files.external("shareIMG.png").delete();
            FileHandle fh;
            do
            {
//                fh = new FileHandle(Gdx.files.getLocalStoragePath() + "shareIMG.png");
                fh = Gdx.files.external("shareIMG.png");
                System.out.println(fh.path());
            }
            while (fh.exists());

            Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);

            PixmapIO.writePNG(fh, pixmap);

            pixmap.dispose();
        }

        catch (Exception e)
        {
        }
    }

    private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown)
    {
        final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);

        if (yDown)
        {
            // Flip the pixmap upside downx
            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);
            pixels.clear();
        }
        System.out.println("SCREENSHOT TAKEN!!!");
        return pixmap;
    }
}

Upvotes: 3

Views: 1270

Answers (3)

Jordan
Jordan

Reputation: 103

For anyone else still trying to get this working, what did it for me was using

fh = new FileHandle(Gdx.files.getExternalStoragePath() + "shareIMG.png";

Instead of the other file handle in the ScreenshotFactory, as Ashwani suggested.

Then the Android code is slightly modified to look like this:

String filePath = new FileHandle(Gdx.files.getExternalStoragePath() + "shareIMG.png").toString();
File file = new File(filePath);

String text = "Look at my awesome picture";
Uri pictureUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");

shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);

startActivity(Intent.createChooser(shareIntent, "Share images..."));

And don't forget the manifest permissions for external storage

Upvotes: 2

Ashwani
Ashwani

Reputation: 1284

Check for the appropriate permission in AndroidManifest.xml add this

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

and i think it will do..'cauz rest of your code is fine according to me...

Upvotes: 1

waxtah
waxtah

Reputation: 306

1) Possible you use wrong code in getScreenshot on lines:

        pixels.clear();
        pixels.put(lines);
        pixels.clear();

2) Look solutions of the same problem: How to use "Share image using" sharing Intent to share images in android?

Upvotes: 0

Related Questions