Native Android screenshot/image sharing in Unity

In continuation to my previous medium post “Native Android in Unity” and “Native Android text sharing in Unity”, will consider another example of native Android screenshot sharing functionality in the unity app.

This post was originally posted at https://agrawalsuneet.github.io/blogs/native-android-image-sharing-in-unity/ and reposted on Medium on 10th Aug 2018.

If you have not read the previous medium posts, I would strongly recommend to read them first. You can read them on the links below.

As the number says, an image gains more attention than a normal text while sharing. It’s better to challenge a player with your own high score screenshot in games instead of sharing just a plain text.

As in the previous post, we saw Android uses an Intent class object to perform any sharing operation and we can use the same Intent class in unity using AndroidJavaClassand AndroidJavaObject .

We can share any image (existing or captured screenshot) in native Android using the same Intent class. As we saw in the previous example of native text sharing, we can set the type of sharing to an Intent object. We will set the type of sharing to “image/png” in our case.

Let’s have a look at its native android code first.

//Java
String screenShotPath = "fireblock_highscore.png";
Uri uriToImage = Uri.fromFile(new File("file://" + screenShotPath));
String shareSubject = "I challenge you to beat my high score in Fire Block";
String shareMessage = "I challenge you to beat my high score in Fire Block. " +
"Get the Fire Block app from the link below. \nCheers\n\n" +
"http://onelink.to/fireblock";


Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);


shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);

shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "Share your high score"));

And the Kotlin code will be

//Kotlin
val screenShotPath = "fireblock_highscore.png"
val
uriToImage = Uri.fromFile(File("file://" + screenShotPath))

val shareSubject = "I challenge you to beat my high score in Fire Block"
val
shareMessage = "I challenge you to beat my high score in Fire Block. " +
"Get the Fire Block app from the link below. \nCheers\n\n" +
"http://onelink.to/fireblock"

val
shareIntent = Intent()
shareIntent.action = Intent.ACTION_SEND

shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage)
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject)
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage)

shareIntent.type = "image/png"
startActivity(Intent.createChooser(shareIntent, "Share your high score"))

Now let’s have a look at how we can use the same native Android screenshot share functionality in a unity app.

Please continue reading at https://agrawalsuneet.github.io/blogs/native-android-image-sharing-in-unity/

That’s all for now. You can read my other interesting posts here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.