- Home /
Saving and loading to/from Android
Hello,
I am developing a project, where I need to first save a screenshot to a file and then read from that file to get a texture. I got everything working correctly on my PC and now I am trying to make a build for Android but I cannot find a way to make it happen. Right now, my code looks as follows: Saving the screenshot to a file:
path = "jar:file://" + Application.dataPath + "!/assets/";
ScreenCapture.CaptureScreenshot(path + _name);
Loading textures from the files (libraryLength is a number of the files, photos[] is an array of textures, while photoNames[] is an array with names of the files)
path = "jar:file://" + Application.dataPath + "!/assets/";
for(int i = 0; i < libraryLength; i++)
{
photos[i] = LoadPNG(path + photoNames[i]);
}
LoadPNG method:
public static Texture2D LoadPNG(string filePath)
{
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath))
{
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
}
return tex;
}
I've tried using many different combinations of paths, both for StreamingAssets and Resources folders but all of them ended up not working on my mobile. The Write Permission is set to External ( SD Card) in the Player Settings.
Answer by Ludwik1526 · Apr 15, 2020 at 08:48 PM
Ok, I have just managed to solve it, if anyone wants the solution:
The line for saving the screenshot is:
ScreenCapture.CaptureScreenshot(name);
And the code for loading the textures from files is:
for (int i = 0; i < libraryLength; i++)
{
photos[i] = LoadPNG(Application.persistentDataPath + "/" + photoNames[i]);
}
This way locates the files in:
Android/data/com.companyname.appname/files