SOLVED! See Answers below.
Hi everyone!
I’ve been looking for answers everywhere but I couldn’t find it.
I’m trying to take a screenshot and upload it to apps. Here’s my code
public void TakeScreenShot(GameObject a){
Vector3 ScreenShotPos = new Vector3(1.7f,-6.95f,0f);
ssName = "PIXMA.png";
if (a != null)
CharaForScreenShot = a;
temp = GameObject.Instantiate(CharaForScreenShot,ScreenShotPos,CharaForScreenShot.transform.rotation) as GameObject;
path = Path.Combine(Application.persistentDataPath,ssName);
print (path);
//delay for screenshot
StartCoroutine(TakeScreenShot());
}
IEnumerator TakeScreenShot() {
// We should only read the screen buffer after rendering is complete
yield return new WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
// Read screen contents into the texture
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
// Encode texture into PNG
byte[] bytes = tex.EncodeToPNG();
Object.Destroy(tex);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(path, bytes);
StartCoroutine(wait(3));
}
private IEnumerator wait(int waitTime){
yield return new WaitForSeconds(waitTime);
if( File.Exists(path) ) {//file exist
WWW loadedImage = new WWW("file://"+path);
if (temp != null) Destroy(temp);
SSImg.texture = loadedImage.texture;
CollectionSceneHandler.instance.waitToShare(path);
CollectionSceneHandler.instance.DisableScreenShotCamera();
}else{//file doesn't exist
print ("file not exist");
}
and I got codes below from Share an image calling external apps on Unity for Andorid
IEnumerator ShareToInstalledApps(string path){
yield return new WaitForSeconds(3);
//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://"+path);
//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);
}
the screenshot work well,. The Image shows when I call it using WWW.
The problem is whenever I try to share the image. it always gets an error.
Please help!