1

I have an app made with unity c# and I need to read a .txt file from de Download folder in android. I search the question everywere but cannot find anything. I went through many forums and they all talk about the persistentDataPath and dataPath but thats not what I need because you cant manually access to them. I already have the scrip to get the dowloand folder path and for read the .txt file. Works fine on unity/windows the script find the download folder and read the .txt but when I run the app on android I cant read the .txt file. I read somewhere that ReadAllText() doesnt work in Android, I tried Resource.Load() and TextAsset but doesnt work neither. I spend the last two days searching for answer but didn't find anything, just people with the same problem and no solutions. Sorry if I add some wrong tag or did something wrong in anyway, tis the first question I do here. Thanks in advance

This is the code that im using to obtain the Download folder path

private static string GetDownloadFolder()
{
    string[] temp = (Application.persistentDataPath.Replace("Android", "")).Split(new string[] { "//" }, System.StringSplitOptions.None);

    return (temp[0] + "/Download");
}

And this is how i read the .txt file (only works on windows and unity editor)

private string[] ReadTxt(string path)
{
    string[] lineas = null;
    path += "/Cuentas.txt";
    lineas = System.IO.File.ReadAllLines(path);
    return lineas;
}
Duu
  • 11
  • 2

1 Answers1

0

For android you can't use Application.persistentDataPath, because its an APK file and not an folder, so you can't access the folder location. Try using:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

You can also look at this: How to store files generated from app in "Downloads" folder of Android?

I hope this helps!

Flof
  • 99
  • 1
  • 6
  • How can I implement Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); this on my code? – Duu Apr 10 '22 at 20:55
  • Because the one you link there its for java and I need it for c# – Duu Apr 10 '22 at 21:04
  • Here, this might work: https://learn.microsoft.com/de-de/dotnet/api/android.os.environment.getexternalstoragepublicdirectory?view=xamarin-android-sdk-12 – Flof Apr 11 '22 at 08:03