Reading xml and texture files in streamingassets folder

Hello everyone.
Currently I’m writing an app which store xml and texture files in project folder. When I build the apk file, I know that the “StreamingAssets” folder that we see in project folder will be compressed into that single apk file.
However, right now I’m facing the issue that I can’t read those files in the that streamingassests in that apk file. Please be aware that I will both be reading and writing those xml and textures will be loaded in real time. I’ve done a quick search for solution, the result is suggesting copying files that will be “manipulated” from Application.streamingAssetsPath to Application.persistentDataPath. I’ve got that code working in Windows, but not in Android.

Regarding building info, MInimum API level is Android 4.1 “Jelly Bean” (API level 16), Device Filter is ARMv7 only, Graohics Level is OpenGL ES 2.0

According to this link, http://forum.unity3d.com/threads/183818-Android-Streaming-Assets, WWW class will be needed to load the streaming assets
the following is my code

IEnumerator assetFileManage_AND()
	{
		string[] xmlFilePath_AND = Directory.GetFiles(Application.streamingAssetsPath + "/xml/");
		string[] xmlFileNamePath_AND = new string[xmlFilePath_AND.Length];
		
		string[] texFilePath_AND = Directory.GetFiles(Application.streamingAssetsPath + "/Texture/");
		string[] texFileNamePath_AND = new string[texFilePath_AND.Length];
		
		for(int i=0;i<xmlFileNamePath_AND.Length;i++)
		{
			xmlFileNamePath_AND[i] = Path.GetFileName(xmlFilePath_AND[i]);					
		}
		
		for(int x=0;x<texFileNamePath_AND.Length;x++)
		{
			texFileNamePath_AND[x] = Path.GetFileName(texFilePath_AND[x]);
		}
		
		
		for(int j=0; j<xmlFileNamePath_AND.Length;j++)
		{
			if(!File.Exists(Application.persistentDataPath + "/xml/" + xmlFileNamePath_AND[j]))
			{
				Debug.Log(xmlFileNamePath_AND[j] + " NOT exists------");
				
				WWW sourceDataXML = new WWW(Application.streamingAssetsPath + "/xml/" + xmlFileNamePath_AND[j]);
				yield return sourceDataXML;
				
				File.Copy(sourceDataXML.text,
						  Application.persistentDataPath + "/xml/" + xmlFileNamePath_AND[j],
						  true);
			} else 
			{
				Debug.Log(xmlFileNamePath_AND[j] + " exists------");
			}
		}
		
		
		for(int y=0; y<texFileNamePath_AND.Length; y++)
		{
			if(!File.Exists(Application.persistentDataPath + "/Texture/" + texFileNamePath_AND[y]))
			{
				Debug.Log(texFileNamePath_AND[y] + " NOT exists------");
				
				WWW sourceDataTex = new WWW(Application.streamingAssetsPath + "/Texture/" + texFileNamePath_AND[y]);
				yield return sourceDataTex;
				
				File.Copy(sourceDataTex.text,
						  Application.persistentDataPath + "/Texture/" + texFileNamePath_AND[y],
						  true);
			} else 
			{
				Debug.Log(texFileNamePath_AND[y] + " exists------");
			}
		}
	}

How should I modify my code to get that working?
Any help will be greatly appreciated.

1 Like

so at last I got it working. The truth is you can’t use Directory.GetFiles() for android system, as the apk is a compressed file, you will never able to get anything out using Directory.GetFiles(). You will have to extract all individual files one by one, and place them wherever you want.

here’s a little example of how I get that workaround working

			string book1Path = "jar:file://" + Application.dataPath + "!/assets/xml/book1.xml";
			string xmlSetUpPath = "jar:file://" + Application.dataPath + "!/assets/xml/xmlSetUp.xml";
		
			WWW book1WWW = new WWW(book1Path);
			yield return book1WWW;
			if(!File.Exists(Application.persistentDataPath + "/xml/book1.xml"))
			{
				File.WriteAllBytes(Application.persistentDataPath + "/xml/book1.xml", book1WWW.bytes);	
			}
			
			WWW xmlSetUpWWW = new WWW(xmlSetUpPath);
			yield return xmlSetUpWWW;
			if(File.Exists(Application.persistentDataPath + "/xml/xmlSetUp.xml"))
			{
				File.WriteAllBytes(Application.persistentDataPath + "/xml/xmlSetUp.xml", xmlSetUpWWW.bytes);	
			}

hope that I can help anyone come across this issue in the future

1 Like

Hi, How can access xml file in StreamingAssets folder in WebGL build uploaded in LMS /web