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

15 Comments

  1. Archit 13 May 2015

    @Naeem
    Be sure to grant write access to External SD drive in the Unity player settings.

  2. tribio 13 May 2015 — Post Author

    @Naeem
    Hi,
    check the code inside the example project, the path should be correct like that, have you granted writing access to the external sdcard?

  3. Naeem 13 May 2015

    Dear i am using a gallery screenshot to capture screenshot.its capture screenshot and save it to following location.”/storage/emulated/0/Android/data/com.Company.NailArt/files/NailArt/”;.
    I copy above code for sharing its opening the whats app but its not showing my picture to send.
    Please tell me where i set path that this code open the picture with whats app.
    thanks.

  4. tribio 11 May 2015 — Post Author

    @Archit
    You are welcome!
    No, I don’t have anything for iOS, I’m sorry…

  5. Archit 11 May 2015

    Thanks for this. Works perfectly!
    Do you have something similar for iOS?

  6. tribio 6 May 2015 — Post Author

    @meta
    Right! Thank you!
    I think it’s the solution…

  7. meta 6 May 2015

    @tribo
    in this second i think i found a solution

    string texture = “logo_menu”;
    Texture2D inputTexture = (Texture2D)Resources.Load(texture);
    string path = Application.persistentDataPath + “/logo.png”;
    if (!System.IO.File.Exists(path))
    {
    byte[] bytes = inputTexture.EncodeToPNG();
    File.WriteAllBytes(path, bytes);
    }

    AndroidJavaClass fileClass = new AndroidJavaClass(“java.io.File”);
    AndroidJavaObject fileObject = new AndroidJavaObject(“java.io.File”, path);
    AndroidJavaObject uriObject = uriClass.CallStatic(“fromFile”, fileObject);

    thx

  8. tribio 6 May 2015 — Post Author

    @meta

    Hi, I think so, but I don’t know the code right now sorry…
    we just need to find the url of the Resources’ folder … if is exists..
    Please let me know if you find a solution before than me! 🙂

  9. meta 6 May 2015

    hi,
    it is possible to share a sprite located within my unity project /Resources ?

    thx

  10. tribio 30 April 2015 — Post Author

    @Tapas
    Hi Tapas,
    it’s commented in the example, just use the EXTRA_TEXT and EXTRA_SUBJECT parameters,
    and maybe for the setType use “text/plain”.
    but not all the apps will receive those params… like the facebook app! :-/

  11. Tapas 30 April 2015

    Hi Tribio,

    Thanks for that brilliant code. I want to share a text just like we can do in android with a subject and body(it contains my app store link). What code change I need in that case?

    Thanks in advance…

  12. Kevin 29 April 2015

    @tribio
    Thanks I had a gut feeling that was it. This works just fine. I have the score on the actual image so its all good. I appreciate you putting this out there saved me a ton of time.

  13. tribio 29 April 2015 — Post Author

    @Kevin
    Facebook works like that, if you want to post text on facebook you must use its own plugin for unity. It’s quite annoying I know!!

  14. Kevin 29 April 2015

    This works perfectly saved me a ton of time. The one thing I have noticed is that when I tell it to post to facebook the subject nor the message actually populate to the Facebook post. I get the image but not the Text which is intended to include score and a link. Any thoughts?

  15. tribio 27 April 2015 — Post Author

    @Dubby
    You are welcome man! 🙂

Leave a Reply

© 2024 Daniele Olivieri

Theme by Anders Norén