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.
tribio 9 March 2015 — Post Author
You’re making me blush guys! I’m really glad that this code helped lots of people! ๐
It would be really appreciated if you can download and rate Speedy Wheel ๐ Thank you!
Danou 9 March 2015
You know, if there were more people like you, the world would be better!! Thanks a lot for sharing freely this code AND an example. I was searching for this for 6 month, and you have this solution.
Thanks again, and continue your good work!
tribio 9 March 2015 — Post Author
wahahahah! Thank you ๐
Ibsen 9 March 2015
You saved a Deadman my brother, thank you very much!
tonypai 12 February 2015
@tribio It’s useful, I need it bad. Thanks really ๐
tribio 11 February 2015 — Post Author
@Rahul Ohri
have you selected the external write access?
Rahul Ohri 11 February 2015
I am getting an error (“Sharing failed , please try again”) , my image is not loading whenever i click share .Please help
tribio 8 February 2015 — Post Author
@Deepak
no workaround sorry, once I just used the facebook plugin for unity ๐
Deepak 7 February 2015
@tribio
Got it, It works.
Have you got any workaround for Facebook intent which doesn’t support text sharing, how do you manage this?
tribio 3 February 2015 — Post Author
Hi Deepak,
there is an example in the zip project, in the file Share.cs, at line 79 ๐
It is commented, under the “option two:” ๐
Let me know…
Deepak 3 February 2015
Thank you for your effort, I have a small problem
I am getting Always and Just once menu , It might be because we haven’t implemented Intent.createChooser , how to do that? and change title too
startActivity(Intent.createChooser(sharingIntent, “Share via”));
Abhinash 15 January 2015
hi,
can we open a new app that is available on Android say Skype or any other ,
if not then it should redirect to Google play.
tribio 15 January 2015 — Post Author
google it, it should be posible I guess! Sorry I can’t help you right now… ๐
tribio 8 January 2015 — Post Author
ok..
I think you should call the ‘StartCoroutine (ShareScreenshot ())’ in the scene you want to be taken, the script must be in the scene you want to take!
So if you want to get the start menu you should attach the script in that scene in Start().
maybe saving in PlayerPrefs a variable TakeScreen = true before the Application.LoadLevel(โstart menuโ);
and in Start() {
if (TakeScreen)
StartCoroutine (ShareScreenshot ())
}
Ben 8 January 2015
@tribio
Oh maybe I forgot to mention. The share screenshot is attached to a button and when the button is touched, it then takes the screenshot. But since your script takes a screenshot of what’s currently on the screen, I’d like to transition first to the start menu with the app title and share that so people know the game’s name.
Ben 8 January 2015
@tribio
May you be a bit more specific?