While learning Unity3D and developing my indie game for Android – Makura – last week, I was trying to set up a button to take a screenshot and share the image opening the default list of Android of the current social apps installed on the device.
I have been stuck for a while, getting errors using AndroidJavaClass and AndroidJavaObject on Unity to call the Java classes of Android.
Once I solved the problem I wanted to share the code on my blog, because I couldn’t find anywhere a solution.
The exact problem was including the jpg of the screenshot as uri for the EXTRA_STREAM parameter of the Intent java object, and let it open properly with other apps.
Below the code in C#:
//instantiate the class Intent AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); //instantiate the object Intent AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"); //call setAction setting ACTION_SEND as parameter intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND")); //instantiate the class Uri AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); //instantiate the object Uri with the parse of the url's file AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file:///sdcard/expl.jpg"); //call putExtra with the uri object of the file intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject); //set the type of file intentObject.Call<AndroidJavaObject>("setType", "image/jpeg"); //instantiate the class UnityPlayer AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); //instantiate the object currentActivity AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity"); //call the activity with our Intent currentActivity.Call("startActivity", intentObject);
I hope this could help someone else!
Don’t hesitate to leave me a comment with your experiences or questions!
**————————–
UPDATE 13 MAY 2014
**————————–
Finally I had the time to make an example project for this utility.
I’ve heard that some of you had troubles to make it work, so I decided to create a kind of tutorial and share it so you can download it and test it!
My computer runs Windows 7 64bit, Unity Free 4.3.4f1, Java jdk1.7.0_55 and jre7.
At the moment the code is working perfectly, inside the zip file you can find “ShareTest.apk” to test the example on your device.
I really hope to help and make many people happy as, I wasnβt expecting this, but it seems that simple plugins like this are still sold! (Why am I sharing this, so?!!!)
Guys, let me know if you have still troubles! π
For Windows users: be sure you have JDK and JRE installed ( link ) and that the JAVA_HOME and Path are correctly set up ( if you don’t know what I’m talking check here ore here)
**————————–
UPDATE 19 JULY 2015
**————————–
IMPORTANT:
Grant the Writing Access to External (SDCard) in PlayerSettings, otherwise you won’t be able to share pictures.
**————————–
UPDATE 23 September 2019
**————————–
This code doesnβt work anymore on the latest Android OS.
You can target Android 6 and it works fine, but if you want to publish it on Google Play, it wonβt be accepted.
Sajid 24 January 2017
Thanks for your tutorial. I can post my screenshot well but no text is shared . Is there any way to share some text and link with that image ?
tribio 17 January 2017 — Post Author
@Jade
Yes, there is! Thanks to ChrisMaire:
https://github.com/ChrisMaire/unity-native-sharing
Jade 17 January 2017
Hi, is it available in ios unity? I want to capture screenshot and share image in ios and android.
Asdeveloper14 30 October 2016
@fran
Is there a way to choose the intentobject? for example only facebook?
can’t you find any way?
tribio 30 October 2016 — Post Author
Hi Asdeveloper14,
if you want to share only on facebook you have to use the facebook plugin. Hope this helps π
Asdeveloper14 30 October 2016
hi
can i set defult app for sharing??
i don’t want user choose desired app for sharing. i want just share my game via facebook .
Ash 19 May 2016
@IES
i could only make is share a ScreenShot – was anyone able to use this to share a resource from the project?
i mean they save an image to the SD card and then display it – but how would you substitued that path to a local image from the project? can assume i use which ever image format and it can be in Resources
Thank you
tribio 2 May 2016 — Post Author
@IES
Yes, the only different is the screenshot, you can take the screenshot save it and then share the image generated using the same url path.
Check out here:
http://docs.unity3d.com/ScriptReference/Application.CaptureScreenshot.html
I hope this helps! π
IES 2 May 2016
Hey I used your code & also saw your app on google play.
Both are quite different. This code doesn’t share the snapshot but only an already existing image from project.
Would be great if you can point me in right direction.
If original code isn’t free anymore that’s fine. I would love to pay for your hard work. But please help me out.
Thanks.
tribio 31 March 2016 — Post Author
HI,
you can use this code, it’s not the best solution but it works fine.
Regarding the screenshot you can easily find how to do it googling.
Sorry I can’t help you more than this! π
agnero 31 March 2016
Hi,
sorry for my lowenglish , but i have a problem whith sharing on facebook , twitter and google+ !
i havent foundasolution on the web to take ascrrenshoot of my level and share it may be with a custum message !
tank you
tribio 16 February 2016 — Post Author
@fran
Hi Fran,
Yeah android is evolving and this script used to work on android 4.4 π
and I don’t think it’s possible to choose the app you want to share with..
but if you find a way to do it, post it here please! π
fran 16 February 2016
It works on facebook and instagram, but the tittle is: Simple sharer, share big files with:
before the tittle was correct but now is some default android tittle.
Is there a way to choose the intentobject? for example only facebook?
Azar 11 January 2016
Thanks for the efforts, is there any way to setup a trigger to check if the user did actually share the app?
tribio 11 January 2016 — Post Author
Hi, I don’t think it would be possible… sorry I don’t have much free time here to improve this plugin π
Jai 10 December 2015
I have apparently fixed the script. I will post it here soon after checking it for possible errors!