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 27 AUG 2014

**————————–

I’ve published the demo on Google Play and removed the apk inside the zip, you can install it to see how it works:

Sharing content (Unity demo) - Google Play

**————————–

UPDATE 19 JULY 2015

**————————–

IMPORTANT:

Grant the Writing Access to External (SDCard) in PlayerSettings, otherwise you won’t be able to share pictures.

Next Post

Previous Post

16 Comments

  1. Dan 27 October 2018

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

    • tribio 27 October 2018 — Post Author

      Thank you, but be careful, it doesn’t work anymore on the latest Android OS (if I’m not wrong from version 7).
      You can target Android 6 and it works fine, but if you want to publish it, it won’t be accepted on the store anymore.
      Always happy to help! Good luck 🙂

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

  3. Andrei Maksimov 4 April 2018

    Do not working on samsung s8

  4. Alexander Zotov 1 February 2018

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

  5. 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, it needs to be updated 😉 but it’s a good start!
            I hope I helped you anyway! 🙂

  6. tribio 28 March 2017 — Post Author

    Hi Alejandro, no it doesn’t 🙂

  7. Alejandro Manzano Diaz 28 March 2017

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

  8. Jason 21 February 2017

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

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

© 2018 Daniele Olivieri

Theme by Anders Norén