How to save an image on Android?
Hi! i'm new in unity and i'm trying to save an image for get it after. I try Application.CaptureScreenshot and the method with Texture2D.ReadPixel, i try to save in persistentDataPath (/data/user/0/my.package.name/files/), in /sdcard/Download/ and in /storage/emulated/0/Download. No method worked. In every manifest of my project i have WRITE_EXTERNAL_STORAGE permission. If i save to persistent data, i don't found it i found only cache of UnityAds, and if I save to Download folder I get access denied.
Anyone can help me?
Here's my code :
Enumerator ScreenShot(){
yield return new WaitForEndOfFrame ();
Application.CaptureScreenshot ("ball.png");
Application.CaptureScreenshot ("/sdcard/Download/ball.png");
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D (width, height, TextureFormat.RGB24, false);
tex.ReadPixels (new Rect (0, 0, width, height), 0, 0);
tex.Apply ();
byte[] bytes = tex.EncodeToJPG ();
File.WriteAllBytes ("sdcard/Download/ball.png", bytes);
File.WriteAllBytes (Application.persistentDataPath + "/ball.png", bytes);
}
Before i check if folders exist : if(!System.IO.Directory.Exists("/sdcard/Download/")){ System.IO.Directory.CreateDirectory ("/sdcard/Download/"); }
if(!System.IO.Directory.Exists(Application.persistentDataPath)){
System.IO.Directory.CreateDirectory (Application.persistentDataPath);
}
And i start IEnumerator with:
StartCoroutine(ScreenShot());
What I'm doing wrong?