Hey! So you all have a played a game where upon completion or game over you get the oppurtunity to share something, perhaps a screenshot along with your score, to friend?

Have you wondered how you go about sharing a screenshot? You may think it is as easy as:

Grab the screen Tell Android to share it It is shared!

Well in thoery I would agree with you. However it isn’t that straight forward. Firstly how do you grab your screenshot? We will be taking the route of grabbing the frame buffer and writing that to a file.

Awesome, yet the next issue that we can only read/write to the applications personal space, when we go to share it with Twitter, for example, we will have permission issues as the Twitter application can’t access the personal space of our game!

“It’s OK” - I hear you cry - “We can just add the READ/WRITE external permissions to our manifest and save the screenshot else where” - Yes, I guess you could do that - but do you know how many people get turned off by a big list of permissions when they go install your app? It’s a lot. I don’t have numbers, but if it wasn’t a lot then Android 6.0 (Marshmallow) would be fixing this with the Runtime Permissions that allow apps/games to ask on the fly.

Anyway, that’s a moo point. We don’t need to ask as we don’t need to use them. We can use something awesome called a “FileProvider”

http://developer.android.com/reference/android/support/v4/content/FileProvider.html

This will allows use to offer files we want our game to allow other apps to access the images we create.

So how do we do this? Glad you asked….

Firstly we update our manifest file to add the provider.

<application> .... <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.our.cool.game.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> .... </application>

Now “com.our.cool.game.fileprovider” doesn’t actually exist as a package in code, we just have to make sure our package is “com.our.cool.game”

Next we add a resource xml file, that I called “file_paths.xml” and put it in res/xml/

<paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="leaderboard" path="screenshots/"/> </paths>

Here I specify the directory that will have allowed access to the other apps.

Next we need the code that will do the do for us…

public void shareScreenShot() { try { File imagePath = new File(activity.getFilesDir(), "screenshots"); File newFile = new File(imagePath, "screenshot.png"); Uri uri = FileProvider.getUriForFile(activity, "com.our.cool.game.fileprovider", newFile); FileHandle fileHandle = Gdx.files.getFileHandle(newFile.getAbsolutePath(), Files.FileType.Absolute); ScreenshotFactory.saveScreenshot(fileHandle); Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Hey this is a cool game"); sendIntent.putExtra(Intent.EXTRA_TEXT, "Hey! check out this leaderboard, can you beat me?"); sendIntent.setType("image/png"); sendIntent.putExtra(Intent.EXTRA_STREAM, uri); sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); activity.startActivity(Intent.createChooser(sendIntent, "OurCoolGame - Share Via")); } catch (Exception e) { Toast.makeText(activity, "Unable to take screenshot at this time.", Toast.LENGTH_SHORT).show(); } }

Actually getting the screenshot…

public static FileHandle saveScreenshot(FileHandle fh) throws Exception { Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); PixmapIO.writePNG(fh, pixmap); pixmap.dispose(); return fh; }

So, what actually happens here.

We create file and URI to that file - This is in the personal area We grab a screenshote and it is written to file. We create our intent and add the information we wish to share. We grant the read uri permission for that intent.

Then we just go for it. And lo and behold we can share an image! Pretty funky no?

Here it is in action for Thug Racer!

Play on Android - https://play.google.com/store/apps/details?id=com.supercookie.speed.android