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. Sajid 24 January 2017

    Thanks for your tutorial. I can post my screenshot well but no text is shared . Is there any way to share some text and link with that image ?

  2. tribio 17 January 2017 — Post Author

    @Jade
    Yes, there is! Thanks to ChrisMaire:
    https://github.com/ChrisMaire/unity-native-sharing

  3. Jade 17 January 2017

    Hi, is it available in ios unity? I want to capture screenshot and share image in ios and android.

  4. Asdeveloper14 30 October 2016

    @fran
    Is there a way to choose the intentobject? for example only facebook?

    can’t you find any way?

  5. tribio 30 October 2016 — Post Author

    Hi Asdeveloper14,
    if you want to share only on facebook you have to use the facebook plugin. Hope this helps πŸ™‚

  6. Asdeveloper14 30 October 2016

    hi
    can i set defult app for sharing??
    i don’t want user choose desired app for sharing. i want just share my game via facebook .

  7. Ash 19 May 2016

    @IES
    i could only make is share a ScreenShot – was anyone able to use this to share a resource from the project?
    i mean they save an image to the SD card and then display it – but how would you substitued that path to a local image from the project? can assume i use which ever image format and it can be in Resources
    Thank you

  8. tribio 2 May 2016 — Post Author

    @IES
    Yes, the only different is the screenshot, you can take the screenshot save it and then share the image generated using the same url path.
    Check out here:

    http://docs.unity3d.com/ScriptReference/Application.CaptureScreenshot.html

    I hope this helps! πŸ™‚

  9. IES 2 May 2016

    Hey I used your code & also saw your app on google play.
    Both are quite different. This code doesn’t share the snapshot but only an already existing image from project.
    Would be great if you can point me in right direction.
    If original code isn’t free anymore that’s fine. I would love to pay for your hard work. But please help me out.
    Thanks.

  10. tribio 31 March 2016 — Post Author

    HI,
    you can use this code, it’s not the best solution but it works fine.
    Regarding the screenshot you can easily find how to do it googling.
    Sorry I can’t help you more than this! πŸ™‚

  11. agnero 31 March 2016

    Hi,
    sorry for my lowenglish , but i have a problem whith sharing on facebook , twitter and google+ !
    i havent foundasolution on the web to take ascrrenshoot of my level and share it may be with a custum message !
    tank you

  12. tribio 16 February 2016 — Post Author

    @fran
    Hi Fran,
    Yeah android is evolving and this script used to work on android 4.4 πŸ™‚
    and I don’t think it’s possible to choose the app you want to share with..
    but if you find a way to do it, post it here please! πŸ™‚

  13. fran 16 February 2016

    It works on facebook and instagram, but the tittle is: Simple sharer, share big files with:

    before the tittle was correct but now is some default android tittle.

    Is there a way to choose the intentobject? for example only facebook?

  14. Azar 11 January 2016

    Thanks for the efforts, is there any way to setup a trigger to check if the user did actually share the app?

    • tribio 11 January 2016 — Post Author

      Hi, I don’t think it would be possible… sorry I don’t have much free time here to improve this plugin πŸ™‚

  15. Jai 10 December 2015

    I have apparently fixed the script. I will post it here soon after checking it for possible errors!

Leave a Reply

© 2024 Daniele Olivieri

Theme by Anders Norén