Search Unity

reading xml and texture files in streamingassets folder

Discussion in 'Android' started by CipherZero, May 20, 2014.

  1. CipherZero

    CipherZero

    Joined:
    Jul 10, 2013
    Posts:
    38
    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
    Code (csharp):
    1.  
    2. IEnumerator assetFileManage_AND()
    3.     {
    4.         string[] xmlFilePath_AND = Directory.GetFiles(Application.streamingAssetsPath + "/xml/");
    5.         string[] xmlFileNamePath_AND = new string[xmlFilePath_AND.Length];
    6.        
    7.         string[] texFilePath_AND = Directory.GetFiles(Application.streamingAssetsPath + "/Texture/");
    8.         string[] texFileNamePath_AND = new string[texFilePath_AND.Length];
    9.        
    10.         for(int i=0;i<xmlFileNamePath_AND.Length;i++)
    11.         {
    12.             xmlFileNamePath_AND[i] = Path.GetFileName(xmlFilePath_AND[i]);                 
    13.         }
    14.        
    15.         for(int x=0;x<texFileNamePath_AND.Length;x++)
    16.         {
    17.             texFileNamePath_AND[x] = Path.GetFileName(texFilePath_AND[x]);
    18.         }
    19.        
    20.        
    21.         for(int j=0; j<xmlFileNamePath_AND.Length;j++)
    22.         {
    23.             if(!File.Exists(Application.persistentDataPath + "/xml/" + xmlFileNamePath_AND[j]))
    24.             {
    25.                 Debug.Log(xmlFileNamePath_AND[j] + " NOT exists------");
    26.                
    27.                 WWW sourceDataXML = new WWW(Application.streamingAssetsPath + "/xml/" + xmlFileNamePath_AND[j]);
    28.                 yield return sourceDataXML;
    29.                
    30.                 File.Copy(sourceDataXML.text,
    31.                           Application.persistentDataPath + "/xml/" + xmlFileNamePath_AND[j],
    32.                           true);
    33.             } else
    34.             {
    35.                 Debug.Log(xmlFileNamePath_AND[j] + " exists------");
    36.             }
    37.         }
    38.        
    39.        
    40.         for(int y=0; y<texFileNamePath_AND.Length; y++)
    41.         {
    42.             if(!File.Exists(Application.persistentDataPath + "/Texture/" + texFileNamePath_AND[y]))
    43.             {
    44.                 Debug.Log(texFileNamePath_AND[y] + " NOT exists------");
    45.                
    46.                 WWW sourceDataTex = new WWW(Application.streamingAssetsPath + "/Texture/" + texFileNamePath_AND[y]);
    47.                 yield return sourceDataTex;
    48.                
    49.                 File.Copy(sourceDataTex.text,
    50.                           Application.persistentDataPath + "/Texture/" + texFileNamePath_AND[y],
    51.                           true);
    52.             } else
    53.             {
    54.                 Debug.Log(texFileNamePath_AND[y] + " exists------");
    55.             }
    56.         }
    57.     }
    58.  
    How should I modify my code to get that working?
    Any help will be greatly appreciated.
     
    Who-am-I likes this.
  2. CipherZero

    CipherZero

    Joined:
    Jul 10, 2013
    Posts:
    38
    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
    Code (csharp):
    1.             string book1Path = "jar:file://" + Application.dataPath + "!/assets/xml/book1.xml";
    2.             string xmlSetUpPath = "jar:file://" + Application.dataPath + "!/assets/xml/xmlSetUp.xml";
    3.        
    4.             WWW book1WWW = new WWW(book1Path);
    5.             yield return book1WWW;
    6.             if(!File.Exists(Application.persistentDataPath + "/xml/book1.xml"))
    7.             {
    8.                 File.WriteAllBytes(Application.persistentDataPath + "/xml/book1.xml", book1WWW.bytes); 
    9.             }
    10.            
    11.             WWW xmlSetUpWWW = new WWW(xmlSetUpPath);
    12.             yield return xmlSetUpWWW;
    13.             if(File.Exists(Application.persistentDataPath + "/xml/xmlSetUp.xml"))
    14.             {
    15.                 File.WriteAllBytes(Application.persistentDataPath + "/xml/xmlSetUp.xml", xmlSetUpWWW.bytes);   
    16.             }
    hope that I can help anyone come across this issue in the future
     
    Who-am-I likes this.
  3. ben06feb

    ben06feb

    Joined:
    Oct 2, 2012
    Posts:
    42
    Hi, How can access xml file in StreamingAssets folder in WebGL build uploaded in LMS /web