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

26 Comments

  1. Annien 23 September 2019

    Hi,
    Thanks, your code does help a lot. It is great!
    Please let me ask a little more, when choosing “save to SD card”, can I choose another destination instead of default folder [saved_SDdata]?

    • tribio 23 September 2019 — Post Author

      Hi Annien, thanks for the feedback! πŸ™‚
      You can choose any path in
      “AndroidJavaObject uriObject = uriClass.CallStatic(“parse”,”file:///sdcard/expl.jpg”);”
      But this code is old, and it doesn’t work anymore on the latest Android OS.

      • Annien 24 September 2019

        Hi,
        Thanks for your reply,
        But you mistook my question a little…
        AndroidJavaObject uriObject = uriClass.CallStatic(“parse”,”file:///sdcard/expl.jpg”); -> this code indicate the input file
        When I clicked sharing, a list of “share to facebook”, “share to twitter”, “share to messenger”,etc would pop up, and one of them is “save to SD card” …
        Chose “save to SD card” and file expl.jpg would be saved to SD-card at [SD-card]\saved_SDdata\pictures\ , this link is created by default on my phone (I tested on Android 6).
        Can I choose a different folder to save to when “save to SD card”?

        • tribio 24 September 2019 — Post Author

          ah! πŸ™‚ sorry about that,
          well… I don’t think you can change that, that’s native.
          But there are some new plugins on the Asset Store, and Andorid is developing new feature constantly, so you might be able to achieve something like that in another way.
          Good luck!

          • Annien 25 September 2019

            I see, Thank you! ( ‒̀ᄇ‒ ́)ﻭ✧

  2. macram 8 August 2019

    anyone can help about unity3d admob.. I follow on tutorials but no ads showing

  3. zak 4 July 2019

    Thaks, it work for me.
    Now I want to share only specific app, How to filter it?

    • tribio 4 July 2019 — Post Author

      Hi Zak,
      I’m sorry, but you can’t do it with this method. πŸ™‚

  4. Dan 27 October 2018

    Hi, this code works amazing. Thanks! You really saved me and my teammate. Nice work! Really wanna say thank you.

  5. Johnny 25 October 2018

    Hi Tribio,
    Thx for your sharing script.
    I tested it on my unity project,but when i clicked the button to share,it couldn’t share through Line and it would show Error Message,but other app like FB messanger it can share correctly.
    I’ve downloaded your App on the Play store,it can share snapshot correctly through Line.
    I checked everything but nothing help.
    I will be appreciated if you have any solution.
    Sorry for my bad English.

  6. Andrei Maksimov 4 April 2018

    Do not working on samsung s8

  7. Alexander Zotov 1 February 2018

    Works perfectly! Thank you very much!
    Used in one of my video tutorial project. Credits were given)

  8. CHRIS 22 January 2018

    Hi, thx for the code.

    for me the screenshot works well, but impossible to share: /
    I took care to change the Writing Access to External, but nothing more happens on my Android device.
    I also look in the comments if someone had the same problem but found nothing.

    my phone does not have an SD card, do I need to change something in the code or unity to make it work for me?

    if you have an idea or a track I am a taker, thank you.

    • tribio 22 January 2018 — Post Author

      Hi Chris,
      it should work, but I didn’t properly tested on the latest android version, have you tried to install the app from the store?
      or have a look at this:
      https://github.com/ChrisMaire/unity-native-sharing

      • CHRIS 22 January 2018

        Great, I tested the demo with the link that you send me and it works perfectly.
        it only remains to integrate it into my app and it should work.
        Thank you very much for your help and sorry to finally not use your own code.
        Bye πŸ˜‰

        • CHRIS 22 January 2018

          Well, in fact it does not work for all sharing methods, but it’s a good start πŸ™‚

          • tribio 22 January 2018 — Post Author

            it’s an old code and it needs to be updated πŸ˜‰
            I hope I helped you anyway! πŸ™‚

  9. tribio 28 March 2017 — Post Author

    Hi Alejandro, no it doesn’t πŸ™‚

  10. Alejandro Manzano Diaz 28 March 2017

    Hi, I have a question, your code receive a callback when you share it?

  11. Jason 21 February 2017

    @tribio
    You can also share your screenshot with some extra data (your app name/icon)

  12. tribio 24 January 2017 — Post Author

    Hi Sajid,
    no, unfortunately this is it.
    Some apps share also the text, some others don’t.
    For example, on facebook, if you want to share also the text you have to use the facebook plugin for unity.
    Good luck! πŸ™‚

Leave a Reply

© 2024 Daniele Olivieri

Theme by Anders Norén