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! ๐
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.
manuel 14 May 2014
Replace
currentActivity.Call(“startActivity”, intentObject);
with
AndroidJavaObject jChooser = intentClass.CallStatic(“createChooser”, intentObject, “YO BRO! DO YOU WANNA SHARE?”);
currentActivity.Call(“startActivity”, jChooser);
To change the classic “share text” of the share window.
homebrewman23 13 May 2014
I made changes to line 57, 70 and 71. Line 57 I removed the image tagging stuff and gave it one name so only 1 screenshot can be stored instead of an unlimited amount. Line 70 and 71 were commented out but they can be used to pre fill out text in fields of whatever the user chooses to share with. I added a little message and a link to my game there. My game has Just been re uploaded to the play store with this script implemented also so feel free to go try it out. I added a little camera icon in each level which fires off the script at the users discretion! Thanks again and I am redirecting people here from Twitter who also need to make this work.
homebrewman23 13 May 2014
@tribio it would be my pleasure! And thank you very much! ๐
tribio 13 May 2014 — Post Author
@homebrewman23
Today I’m happy guys! ๐
I’ve installed your game, but I’ll play it and leave a comment later.
Soon I will release a new game: Speedy Wheel, I hope you will help me to share it ๐
homebrewman23 13 May 2014
@tribio
I’m using PC but I tested it on my android device. Haven’t had a chance to disect the code yet, I’m going to add a predefined message in the field also with a ref. To my game and I will post the additions to the script back here
homebrewman23 13 May 2014
My Friend thank you so much! It works flawlessly and allows sharing via whatever somebody chooses as well! I will certainly include you in the credits of my game. Here is the game by the way for anybody interested. Its a hill climb racing but 3d style game. I will tweet about this also because i asked people on twittetR to help figure this out and it will help many people i think. Thanks so much! https://play.google.com/store/apps/details?id=com.failcodegames.EatDirt
tribio 13 May 2014 — Post Author
oh!! good to know that on mac it works! Thank you for the feedback!!!
manuel 13 May 2014
Oh, im using a MAC
manuel 13 May 2014
This demo works for me. Unity PRO 4.3.4.
I really dont know the reason of the error that I had last time.
THX!
tribio 13 May 2014 — Post Author
@homebrewman23
Itโs unity free! does the example work?
homebrewman23 13 May 2014
you know what I just realized, are you using Unity Pro? There is a limitation on taking screenshots for non Pro users, I wonder if that’s what throws the errors. I’ll assume that you’re using the latest version of unity
tribio 13 May 2014 — Post Author
Hi homebrewman23, sorry to hear that some people have troubles, for me it is still working fine! I’ve just done a test project so you can download it and play with it ๐ The post will be updated in a bit.
homebrewman23 13 May 2014
This Code no longer works in the latest version of Unity. There are errors about the static type inference On the 3 lines that use it. At the end of the day, its unfortunate that something so simple cant be done without a plugin that cost 20 or so dollars to add to the never ending list of money it cost to make a game, that you may never get back. Its not really legal to share the scripts you get when you buy a working plugin so I guess there is a monopoly on the information. Thank you guys for sharing what you know anyway!
tribio 9 April 2014 — Post Author
but it should! …. no idea why it doesn’t…
manuel 9 April 2014
Hi, Im not sure how this stuff works so probably my questions are silly.
If I add this script to a void project it should works?
I add this script on the camera:
__________________________
using UnityEngine;
using System.Collections;
public class prova : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log(“A”);
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);
}
}
__________________________
but it doesnt work. I read this on logcat:
04-09 11:48:20.400: I/Unity(10180): AndroidJavaException: java.lang.NoSuchMethodError: no method with name=’startActivity’ signature='(Landroid.content.Intent;)Ljava/lang/Object;’ in class Lcom/unity3d/player/UnityPlayerNativeActivity;