I may just be missing something obvious, but I am having an issue with a Unity script that shares a line of text and an image to external apps (e.g. Twitter).
I can send the text perfectly fine, but the image (which is supposed to be an in-game screenshot) will not appear as an attachment to the message.
I’ve tried changing the filepath
string to be the filepath of a pre-existing PNG file on my phone, in case the screenshot section of the code doesn’t work, but I’m still having the same issue. I’ve also tried changing the File://
to be all lowercase (which causes the code to stop working altogether) or changing it to Content://
(which has the same issue where there’s no image attachment).
A lot of the tutorials I’ve found online are fairly old, so it’s possible that there’s something in my code that is simply out-of-date.
If anybody has some suggestions on what to change, that’ll be great. Thank you!!
public IEnumerator AndroidShare()
{
isProcessing = true;
yield return new WaitForEndOfFrame();
string shareMessage = "Test shareMessage";
string screenshotName = (DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")) + ".png";
ScreenCapture.CaptureScreenshot(screenshotName, 2);
string filePath = Path.Combine("File://" + Application.persistentDataPath, screenshotName);
yield return new WaitForSecondsRealtime(1f);
if (!Application.isEditor)
{
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", filePath);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareMessage);
intentObject.Call<AndroidJavaObject>("setType", "image/png");
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject chooser = intentClass.CallStatic<AndroidJavaObject>("createChooser", intentObject, "Share Picture");
currentActivity.Call("startActivity", chooser);
yield return new WaitForSecondsRealtime(1f);
}
yield return new WaitUntil(() => isFocus);
isProcessing = false;
}