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. tribio 8 April 2014 — Post Author

    Yeah you are right!!!
    and sorry but I don’t have anything for iOS, I’m not really a fan of Apple πŸ˜› …
    but if you find something please share it here! πŸ™‚

  2. manuel 8 April 2014

    @tribio
    You have any information/link/tip about do the same thing on ios?
    In my opinion, this thing is very underrated by the newbie app creators, and because of this (myself included) they always buy plugins just to post a picture on fb and tw.

  3. tribio 8 April 2014 — Post Author

    @manuel
    Hi Manuel,
    Thank you!
    this works only on android πŸ˜‰

  4. manuel 8 April 2014

    This is great! Im gonna to try it. But it works only for android or for ios too?

  5. tribio 25 February 2014 — Post Author

    Hi Geran,
    thanks for sharing your code! Yes!
    In my example the code works if you have an image already into your sdcard,
    you got a screenshot using “Application.CaptureScreenshot” good!
    To get a screenshot I used “Texture2D.ReadPixels” and then compress it with JPEG Encoding plugin, (I think I’m going to write a post for that!)
    Thank you!

  6. Geran 24 February 2014

    Hi Tribio

    I needed to modify your code a little but got it working πŸ˜€

    Thank you so much for this! It gave me a great place to start πŸ™‚

    Here is my code I used:


    public void ShareScreenShot() {

    Application.CaptureScreenshot(“expl.png”);

    AndroidJavaClass intentClass = new AndroidJavaClass(“android.content.Intent”);

    AndroidJavaObject intentObject = new AndroidJavaObject(“android.content.Intent”);

    intentObject.Call(“setAction”, intentClass.GetStatic(“ACTION_SEND”));

    AndroidJavaClass uriClass = new AndroidJavaClass(“android.net.Uri”);

    AndroidJavaObject uriObject = uriClass.CallStatic(“parse”,”file://”+Application.persistentDataPath+”/expl.png”);

    intentObject.Call(“putExtra”, intentClass.GetStatic(“EXTRA_STREAM”), uriObject);

    intentObject.Call(“setType”, “image/png”);

    AndroidJavaClass unity = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);

    AndroidJavaObject currentActivity = unity.GetStatic(“currentActivity”);

    currentActivity.Call(“startActivity”, intentObject);

    }

    Thank you so much πŸ™‚

    Geran Bronn

  7. tribio 24 February 2014 — Post Author

    Hi Geran,
    you can copy and past the code even in the Start() method in any if your scripts ant test it!
    It doesn’t work on the unity eidtor, only on the mobile and the image has to be in the path on your mobile, in that case under sdcard folder (“file:///sdcard/expl.jpg”).
    Let me know! πŸ™‚

  8. Geran 23 February 2014

    Hi there

    I am a relative newbie with Unity and C#

    I am also looking at making a function like this in my app

    I am not sure how to use your code in my app

    Can you please give me an example on how to integrate this into my app

    It would be greatly appreciated πŸ™‚

    Geran

  9. DylanW 7 February 2014

    Yes–sorry for all the posts. The link I added worked for me. I’m using Unity 4.3, so I figure this may be something new.

  10. tribio 7 February 2014 — Post Author

    Hi Dylan,
    so have you sorted it out? πŸ™‚
    for me this code still works, and I didn’t need to add anything in the manifest.
    btw thanks for the link, could be useful!

  11. DylanW 7 February 2014

    @DylanW
    Sorry–that should have read GetStatic (less than) AndroidJavaObject (greater than). I think it stripped it out as HTML.

  12. DylanW 7 February 2014

    Thanks for the code–this is almost exactly what I’m trying to do right now. However, I’m running into two problems.

    First, Unity can’t infer the type for the GetStatic calls. I’ve tried converting these lines to read intentClass.GetStatic(“…”). It compiles, but I’m not sure I’m using the right type here.

    Second, when I run it, it fails giving me the error: no method with name=’setAction’ signature='(Ljava/lang/String;)V’ in class Landroid/content/Intent.

    Is there something I need to include in the Android manifest to make this work? Or has something changed in Unity since then that I’m not accounting for?

  13. tribio 24 November 2013 — Post Author

    Hi Mguel,
    I’m happy to have helped someone! πŸ™‚
    The code I used is for MonoDevelop, but I think you can use it in Eclipse, you just need to convert the code for javascript, and I don’t think you need to create any plugin.
    Sorry but I’m a junior as well, and I didn’t try on Eclipse.
    But please let me know if you can make it work and how!
    And most important, if possible, share your project once is done!

  14. Mguel 22 November 2013

    Hi Tribio!

    First of all, thank you very much for this great post. As you said, there are not information about this. I tried on Unity Forums, StackOverflow, etc and nothing.

    I am developing an app, to do exactly the same, take a picture with the camera, and then I want to let the user to post it via his installed social apps.

    But I can do that, because I’m not a very good coder and there are no information or tutorial about this.

    The code you post it here, is to do in Eclipse or in Unity? Is neccesary to create a plugin in Eclipse?

    I would greatly appreciate if you could expand a little more this information. Like a little minitutorial or similar.

    Thank you very, very, very much!

Leave a Reply

© 2024 Daniele Olivieri

Theme by Anders Norén