Hi there ! I’m having a little problem with a project i’m doing. The game needs to access the Android Download Folder to load a XML file.
I succesfully can read the XML file with all the tags using the StreamingAssets, but i really need to access the Download folder in Android (that is where the XML files is going to be), because every time the game starts, is going to fetch the XML (that is going to be modify from a PC with other software).
I’m using this code right now, but i can’t find the path to the Download folder. (The game runs on android 4.1 and newer versions).
IEnumerator Start()
{
this.debuArchivo.text += "\n"+ "Antes del WWW";
//Load XML data from a URL
//string path = "jar:file://" + Application.dataPath + "!/assets/xml/XMLPrueba.xml";
string path3 = System.IO.Path.Combine(Application.streamingAssetsPath,"XMLPrueba.xml");
this.debuArchivo.text += "\n"+ "Path: "+path3;
WWW www = new WWW(path3);
//Load the data and yield (wait) till it's ready before we continue executing the rest of this method.
yield return www;
this.debuArchivo.text += "\n"+ "Despues del Yield";
if (www.error == null)
{
//Sucessfully loaded the XML
this.debuArchivo.text += "\n"+"Cargo el XML " + www.data;
//Create a new XML document out of the loaded data
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(www.data);
//Point to the book nodes and process them
ProcesarDatos(xmlDoc.SelectNodes("Objetos/Objeto"));
this.debuArchivo.text += "\n"+"Salio";
}
else
{
this.debuArchivo.text += "\n"+ "ERROR: "+www.error.ToString();
}
}
For my phone download folder is located under:
“/storage/sdcard/downloads”
Tho, that varies on each phone, so you need a way to get the androids internal storage path dynamically. Hardcoding your phones path would result in incompatibility for many other phones. Have a look on how to create native android plugins.
Had the same problem, isnt that difficult to write an native one for it
Even if you knew the actual location, are you able to read/write from Unity without a native plugin (to something outstide of Application.DataPath for example)?
Maybe i’m complicating things, i need the game to use a XML File, this file is going to be created/modificated in a PC to later move it to the android phone-
No, we understand what you are saying… the problem is you have no way of knowing what the directory is. And you can only find this out by using a native plugin… does that make sense?
If you want the xml not to be placed under your application.datapath folder, you need an plugin to determine your phones storage locations. Those are different on most phones and theres no out of the box api to get those paths in unity, as far as i know. You just need it for getting the path, read/write can be done within unity itself.
Yep, but its not user friendly, tho its possible. If your app is installed on internal/external storage, user would have to look for something like: “/yourstorage/android/data/com.your.packagename/” and paste the xml there. That should work then when using Application.dataPath.
And also, if it doesnt work thy this, when savin to application datapath&&using System.io, try to add “files” to your name.
An example, if you wanna load “Application.dataPath”+“/test.xml”
your actual file text.xml should named filestest.xml on your dataPath.
Application.dataPath is refering to the data folder. And im definetly ably to write to it via System.IO. Also via copy/paste on pc. Tho, thast just the data folder, not the actual location where the apk gets installed.
Found a way, to get the internal download folder without an native plugin. Tho its a bit hackerish.
You need to set:
BuildSettings->PlayerSettings->Android->OtherSettings->InstallLocation to “ForceInternal”
Than create an static string in your script:
public static string GetDownloadFolder () {
string[] temp=(Application.persistentDataPath.Replace ("Android","")).Split (new string[] { "//" },System.StringSplitOptions.None);
return (temp [0] + "/Download");
}
That will return the download folder, you could then get the xml path by:
xmlpath = GetDownloadFolder ()+"/XMLPrueba.xml";
That actually works for me, but yeah, thats kinda hackerish imo.
Edit: for your example above, if you wana use www, you need to add file:// to the path. Tho i would recomnend using System.IO: