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 6 August 2014 — Post Author
@Raja
Hi Raja,
have you tried to install the apk in the zip and see if it works?
the project still works fine.. maybe some wrong config in your machine…?
Sorry I can’t help much…
Raja 6 August 2014
I runned your apk file but it didnt installed,which is in zip file. then i rebuilded the application and installed.when i was clicked on the share button,the button is hiding but nothing is happened i don’t know why it happens. pls let me know.
tribio 27 July 2014 — Post Author
@Amir
You are welcome!
For InApp Purchase you can use Soomla or OpenIAB plugins π
Amir 27 July 2014
Thanks for your great Script
It was which i was looking for
may I ask you something?
what did you about In App Purchase?
its me new problem
tribio 25 July 2014 — Post Author
@apieiron
Hi Apieiron,
it shouldn’t restart… I’ve never had this problem! Sorry, I can’t be helpful…
apieiron 25 July 2014
code is working thank you. I want to ask after share picture ,my unity app is restarting and i dont understand why. Can you help me ?
tungnguyendev 21 July 2014
Thanks π
tribio 30 June 2014 — Post Author
@Trio4
You are welcome! π
Trio4 30 June 2014
ah, yes, it worked after I write it to external. thank you for your help π
for the record this is how I write the pathName.
var pathName = string.Format (“{0}/HIGHSCORE.png”,Application.persistentDataPath);@tribio
tribio 28 June 2014 — Post Author
@athar
Thank you Athar!
Makura is left like that for the moment…
on my free time I’m working only on Speedy Wheel now π .
For the moment it is completely free, but I’m thinking to add in-app purchasing for some extra gadgets or slots in the game… I’ll see… not much time to work on it now..
Good to know that you sorted out the problem π
athar 28 June 2014
and hey i played your game Makura!! awesome graphics!! and will buy your original as soon as it will be released…
athar 28 June 2014
its working, thanx.
just remind me what was the problem before.
tribio 28 June 2014 — Post Author
@athar
Hi Athar,
are you talking about my project? it gives you that problem?
if yes, try to copy the apk directly form the zip file on your mobile and install it…
and let me know! π
athar 28 June 2014
hey ,thanks for this useful post ,but i got a problem ,when i press the share button and select the app with which it is to be shared. I get a error file not supported on my samsung galaxy s3.
and when i saved it in sdcard i had a broken image.
any help would be appreciated .
and thanx.
tribio 27 June 2014 — Post Author
@Trio4
maybe you have already the rights to read/write because I guess you already save your image to share somewhere, anyway is in PlayerSettings –> Other Setitngs –> Write Access External.
But you need this only if you write on the external…
Anyway I guess the problem is in the path of your image, print the “pathName” on screen and see if the image is in the correct folder on your andorid, use ES File Explorer or similar app…
or let me see how you set your pathName π