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 7 December 2015 — Post Author

    Good call man!
    I’ll look into it, but right now I’m really busy! Can someone fix the script? ๐Ÿ™‚

  2. Oli 7 December 2015

    Can you add Android FileProvider. I need image share without any permissons on Android 6.0 devices.

    http://stackoverflow.com/questions/32981194/android-6-cannot-share-files-anymore

  3. tribio 2 December 2015 — Post Author

    @Jai
    Thanks!
    I know the problem with facebook, and the solution is using its own unity plugin! (good luck!)
    Facebook doen’t allow to share an image like that anymore.
    I don’t know the problem with Instagram, maybe same issue? Sorry I can’t help much… ๐Ÿ™

  4. Jai 2 December 2015

    Hi @tribio
    Thanks for the code. You saved my day but there is a problem I am facing when using your code. When I try to share the image to Whatsapp it works fine, however, when I try any other app, Facebook/Instagram for an instance, it doesn’t load the image. In the case of Facebook a simple post to timeline dialog box appears without the image attached to it and Instagram just crashes saying “unable to load image”.

  5. hotaru 28 November 2015

    @tribio
    how can i delete image after share ?

  6. TalhaDX 6 November 2015

    What if i want to share a specific Texture within my Unity game like a scorecard or any other pre saved Texture??

  7. Sergii 28 October 2015

    Hello. I have tried your code, I don’t know why, but still I cannot see my screenshots in gallery and these screenshots exist in my path

  8. tribio 28 September 2015 — Post Author

    @Tai
    Yes, it will be deleted once uninstalled the app ๐Ÿ˜‰

  9. Tai 27 September 2015

    One thing about your example: it write the image to player storage but doesn’t delete it after, and because you save it in persitent data path it hidden from the phone gallery, player can’t manually delete it

  10. pete 11 September 2015

    Chris M :
    I found this post really useful so I combined it with iOS sharing and put it up on GitHub. I have credited you with the Android portion, let me know if thatโ€™s not OK. Thanks for sharing! https://github.com/ChrisMaire/unity-native-sharing

    Awesome exactly the update it needed. One thing, how would you temp disable the canvas when screenshotting, so that you dont see the share button?

  11. pete 11 September 2015

    pete :
    This doesnt share a screenshot as you say it does, it shares a predefined image which is VERY different and misleading. Also why is is built without the new GUi system?

    Ok nevermind it does. But still an issue with the uGUI, as if you put a canvas and button in, it screengrabs those as well. Also can you alter the close to work with onclick instead of a texture?

    Thanks

  12. pete 11 September 2015

    This doesnt share a screenshot as you say it does, it shares a predefined image which is VERY different and misleading. Also why is is built without the new GUi system?

  13. tribio 19 August 2015 — Post Author

    @basit
    Hi Basit, sorry but I don’t have the time to do it! did you grant the writing access?

  14. basit 19 August 2015

    hi tribio
    can u make tutorial
    i open your project and build ,its work
    but when i make new one nothing happen when i click share
    i use image of button , and also make button inside canvas, still not work

  15. tribio 15 August 2015 — Post Author

    @Abhishek
    Download the example, you can also share text ๐Ÿ˜‰

Leave a Reply

© 2024 Daniele Olivieri

Theme by Anders Norén