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

16 Comments

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

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

  3. tribio 9 March 2015 — Post Author

    wahahahah! Thank you ๐Ÿ˜€

  4. Ibsen 9 March 2015

    You saved a Deadman my brother, thank you very much!

  5. tonypai 12 February 2015

    @tribio It’s useful, I need it bad. Thanks really ๐Ÿ˜€

  6. tribio 11 February 2015 — Post Author

    @Rahul Ohri
    have you selected the external write access?

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

  8. tribio 8 February 2015 — Post Author

    @Deepak
    no workaround sorry, once I just used the facebook plugin for unity ๐Ÿ™‚

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

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

  11. 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”));

  12. 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… ๐Ÿ™‚

  13. 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 ())
    }

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

  15. Ben 8 January 2015

    @tribio
    May you be a bit more specific?

Leave a Reply

© 2024 Daniele Olivieri

Theme by Anders Norén