Daniele Olivieri

3D Digital Artist & Unity Developer

Share an image calling external apps on Unity for Andorid

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! πŸ™‚

Sharing Example Code - in Unity for Android

Sharing Example

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.

Next Post

Previous Post

15 Comments

  1. 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…

  2. 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.

  3. tribio 27 July 2014 — Post Author

    @Amir
    You are welcome!
    For InApp Purchase you can use Soomla or OpenIAB plugins πŸ˜‰

  4. 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

  5. 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…

  6. 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 ?

  7. tungnguyendev 21 July 2014

    Thanks πŸ˜€

  8. tribio 30 June 2014 — Post Author

    @Trio4
    You are welcome! πŸ™‚

  9. 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

  10. 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 πŸ˜‰

  11. 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…

  12. athar 28 June 2014

    its working, thanx.
    just remind me what was the problem before.

  13. 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! πŸ™‚

  14. 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.

  15. 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 πŸ™‚

Leave a Reply

© 2024 Daniele Olivieri

Theme by Anders Norén