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.
Dubby 27 April 2015
Great help! i was seaching for this for a long time, i thought i must using android studio for that function .. but i dont know if it actually could be done in unity itself. thanks daniel 😀
tribio 18 April 2015 — Post Author
@Ian
Whereever you like to start sharing an image… a button function I guess!
Check the example project 😉
tribio 18 April 2015 — Post Author
@domy
change path and use one in your project 😉
Ian 17 April 2015
Where do I put this code? Start function or do you make a new one?
domy 17 April 2015
Thank you for sharing this guide , I would like to figure out how to change the image to share
thanks
Premal 16 April 2015
Hi,
Thanks a lot for creating this awesome plugin.
I am able to make it work perfectly. Now I want to redirect user to google play if he clicks on screenshot posted on facebook.
Will you please let me know how can I achieve this???
Thanks a lot again in advance.
tribio 16 April 2015 — Post Author
Hi Premal,
I don’t think it is possible to do, just add a short link on your message 😉
tribio 8 April 2015 — Post Author
Hi, sorry for the late reaply guys I was in holidays last week! 🙂
Anyway, already tried to grant the Write Access to “External (SDCard)”:
in Player Settings–> Other Settings
Let me know. 🙂
Abdul 5 April 2015
Hi I am using this but i get a blackscreen as if no screenshot was taken any ideas?
Michele 4 April 2015
@Michele
ho scoperto questa mattina che se buildo il tuo esempio funziona perfettamente sia nell’editor che su android, il mio funziona sull’editor mentre su smartphone non salva l’immagine
Michele 3 April 2015
ciao, ti ringrazio molto per questo script, ma mi è sorto un problema:
lo share sembra funzionare ma non carica l’immagine, perchè non la riconosce o non la trova, penso sia un problema di path, ma non capisco dove stia l’errore.
io ho la scena in una cartella “scenes”, e lo script in una cartella “scripts”,
string destination = Path.Combine(Application.persistentDataPath,System.DateTime.Now.ToString(“yyyy-MM-dd-HHmmss”) + “.png”);
File.WriteAllBytes(destination, dataToSave);
AndroidJavaObject uriObject = uriClass.CallStatic(“parse”,”file://” + destination);
l’errore è qui? o sai dove potrebbe essere se non qui?
Olivier 1 April 2015
Thanks very much, it works like a charm and takes only in a few minutes to implement !
tribio 31 March 2015 — Post Author
@Erfan
Hi, it’s strange, because I have in the choose list “Messaging” that send the content as sms or mms with the image attached! Maybe it depends from some preferences on the mobile,
I’m on Nexus 5, maybe it’s this the reason? Lokk at this my screenshots I just took:
Screenshot one Screenshot two.
Is that what you mean?
Erfan 31 March 2015
Hello. first I want to Tank you Vary Much.
second, how can i send this things via SMS ( just text) ?
I mean in the action selection menu all the software are exist such as viber , bluetooth and …
except sms .
thanks
Avi Dwivedi 25 March 2015
Hey Daniel
Thanks worked great for me.
Any way to do it in iOS?
tribio 26 March 2015 — Post Author
Hi Avi, you are welcome!
Sorry but I’m not really an “IOS guy” :p
Tim Rachor 19 March 2015
Awesome! Thanks a lot, works perfectly and is sooo easy to implement. Couldn’t believe it. ^^