Reputation: 13
I use this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class ShareButton : MonoBehaviour {
public void ClickShare()
{
StartCoroutine(TakeSSAndShare());
}
private IEnumerator TakeSSAndShare()
{
string timeStamp = System.DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss");
yield return new WaitForEndOfFrame();
Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
ss.Apply();
string filePath = Path.Combine( Application.persistentDataPath, "Tanks" + timeStamp + ".png" );
File.WriteAllBytes( filePath, ss.EncodeToPNG() );
Destroy( ss );
}
}
to save screenshot on button press. How can I save it in some folder so it could be found in Android gallery?
Upvotes: 1
Views: 5192
Reputation: 566
In your current solution you would be able to find the screenshot in android/data/com_your_application/files
and you probably want to save it in another place.
To save your screenshot in: /storage/emulated/0/DCIM/
You can use Unity's AndroidJavaClass and Object to get the path:
private string GetAndroidExternalStoragePath()
{
if (Application.platform != RuntimePlatform.Android)
return Application.persistentDataPath;
var jc = new AndroidJavaClass("android.os.Environment");
var path = jc.CallStatic<AndroidJavaObject>("getExternalStoragePublicDirectory",
jc.GetStatic<string>("DIRECTORY_DCIM"))
.Call<string>("getAbsolutePath");
return path;
}
This will give you the path to the public DCIM folder in Android and you can then pass that path into your File.WriteAllBytes
call
Remember to set write permission to External(SDcard) in Player Settings for Android.
If you don't want your screenshots to appear in the root of the DCIM folder you can simply append a directory to the path.
Upvotes: 3