Answer the question
In order to leave comments, you need to log in
How to share a screenshot on android?
Good time! For my game for android, I made the function of sharing the result of the game, but there was a problem: the text works fine, the screenshot is saved, but not transmitted. The code is under the spoiler, help me figure out what's wrong, please
using System.Collections;
using UnityEngine;
using System.IO;
public class ShareResult : MonoBehaviour
{
private string screenshotFilename = "ScreenShotPintaResult.png";
public void AndroidMediaSharingClick()
{
ScreenCapture.CaptureScreenshot(screenshotFilename);
StartCoroutine(SaveAndShare(screenshotFilename));
}
IEnumerator SaveAndShare(string fileName)
{
yield return new WaitForEndOfFrame();
string shareMessage = "Test msg";
string subject = "Test subject";
string title = "Test title";
string path = Application.persistentDataPath + "/" + fileName;
AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
intentObject.Call<AndroidJavaObject>("setType", "image/PNG");
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TITLE"), title);
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), shareMessage);
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject fileObject = new AndroidJavaObject("java.io.File", path);
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("fromFile", fileObject);
bool fileExist = fileObject.Call<bool>("exists");
Debug.Log("File exist : " + fileExist);
if (fileExist)
{
intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
}
AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
currentActivity.Call("startActivity", intentObject);
}
}
Answer the question
In order to leave comments, you need to log in
Because on newer androids you can't give the file:/// URI you get. You need to use FileProvider .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question