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.
Trio4 27 June 2014
hmm, EXTRA_TEXT seems to be working. so, maybe it’s the rights to access after all. Can you tell me how to make sure it has the right?
PS.
I tried using your sharing example and it worked fine.
so, I’m even more confused as to why it’s not working on mine.
@tribio
tribio 26 June 2014 — Post Author
@Trio4
I don’t know.. maybe the pathName is incorrect for some reason? are you sure to have the rights to acces to the path? …
maybe try to remove the EXTRA_STREAM and leave just an EXTRA_TEXT, to see if it works..
Trio4 26 June 2014
Hey! so, I tried using the code you posted here.
I can get the list to show, but when I actually tried to use one of them, it comes up with an error for some reason. Do you have any idea as to why?
Details can be seen on this site. http://stackoverflow.com/questions/24425689/unity-share-button-implementation
tribio 25 June 2014 — Post Author
@Ovi
happy to help! π
Ovi 25 June 2014
Thank you very very much for sharing this..
tribio 26 May 2014 — Post Author
@homebrewman23
Great!!! π Good luck dude!!!
homebrewman23 26 May 2014
OK my friend if I find a way to do it, which I will I will most certainly return the information to you! @tribio
tribio 26 May 2014 — Post Author
Thank you kyle!!
Sorry, but I don’t know if it’s possible to check that…
I wish it could be done! I’d like to do exactly the same!!! π
kyle 26 May 2014
This is working fantastic for my game Eat Dirt for android and I thank you again! I know I am probably pushing my luck by asking another question on this but here goes. Is there a way to check if the user actually sent the screenshot or just backed out of the intent dialog? I want to integrate a reward system for sharing that pays the user in game currency to do it. There should be away to check for if the image was sent, or to check if the user hit the back button to return to the game. You will be in the credits to my game for what you have done for me already by the way! π
tribio 26 May 2014 — Post Author
You are welcome! π
Yana 26 May 2014
this is great ! Thank you so much .
Been looking for a good example .
Only thing i needed to change was to add on this line after GetStatic:
intentObject.Call(“putExtra”, intentClass.GetStatic(“EXTRA_STREAM”), uriObject);
Thanks again π
manuel 14 May 2014
uao, mystery solved π
I confirm that my code already had the ββ π
tribio 14 May 2014 — Post Author
OH my god!!! I just realize that the code in the post is missing “<AndroidJavaObject>” everywhere!! WordPress what are you doing!?!?!?!
Anyway I’ve fixed the code…
tribio 14 May 2014 — Post Author
Thank you manuel!
I had to add “<AndroidJavaObject>” after “CallStatic”, maybe in windows is like that…
I’m going to add your code in the example project π I guess you won’t mind!
manuel 14 May 2014
oh, this removes the option “always use this” or “just this once.” If you know how to get this back let me know