Accessing binary file on Android via WWW

Hi,

I am trying to access a binary file on Android. In the Unity project, I put the file in the StreamingAssets folder. At first, I used the following lines.

BinaryFormatter bf = new BinaryFormatter();
string path = "jar:file://" + Application.dataPath + "!/assets/";
FileStream file = File.Open(path + "filename.txt", FileMode.Open);
objectList = (List<AClass>)bf.Deserialize(file); // converting file into objectList variable, which is a list of AClass objects
file.Close();

However, I got the following error.

I/Unity (17624):
IsolatedStorageException: Could not
find a part of the path
“/jar:file:/data/app/com.CompanyName.ProjectName-1.apk!/assets/filename.txt”

Now I found that the correct way is to use WWW, as suggested here and here. But I am not sure how to convert the data into List. That is to say,

// filePath = path + "filename.txt"; although a txt, it is a binary file
IEnumerator LoadFileOnAndroid(string filePath)
{
	WWW file = new WWW(filePath);
	yield return file;
	// What to write here? objectList = ...?
}

Thank you.

UPDATE (27 May 2015):
I created the file from another application, which also has its own objectList. AClass has System.Serializable attribute. This list was serialized and then saved to a file.

The code to save is as follows.

public class AnotherApp : MonoBehaviour {

	public List<AClass> objectList;
	
	public void SaveToFile()
	{
		BinaryFormatter bf = new BinaryFormatter();
		FileStream file = File.Create(Application.persistentDataPath + "/filename.txt");
		bf.Serialize(file, objectList);
		file.Close();
	}
}

On PC, the file can be loaded correctly via deserialization similar to the first code I wrote above.

So I found out about MemoryStream here, and solved the problem with the following code.

IEnumerator LoadFileOnAndroid(string filePath)
{
	WWW file = new WWW(filePath);
	yield return file;
	MemoryStream ms = new MemoryStream(file.bytes);
	BinaryFormatter bf = new BinaryFormatter();
	objectList = (List<AClass>)bf.Deserialize(ms);
	ms.Close();
}

I am trying to access a binary file on
Android. In the Unity project, I put
the file in the StreamingAssets
folder. At first, I used the following
lines.

First, just use the Application.streamingAssetsPath field that Unity provides, on Android it will return the URL.

Now I found that the correct way is to
use WWW, as suggested here and here.
But I am not sure how to convert the
data into List.

Yes you will have to use the WWW class to get it, this is also covered in the documentation for Application.streamingAssetsPath.

You want to convert the data into a List, but you haven’t described what the internals of the text file look like… Is it delimited? Is it one entry per line and you want a List of string that each element in the List represent a line? Lets go with that one.

Just an example, i did not try it but should be just about right.

using System;
using System.Collections;
using System.Text;
using System.IO;

public class SomeClass : MonoBehaviour
{
  public List<string> FileLines = new List<string>();

  public void ParseFileIntoCollection(WWW wwwObj)
  {
    string line = null;

    if (wwwObj != null && !string.IsNullOrEmpty(wwwObj.text))
      using (StringReader r = new StringReader(wwwObj.text))
      {
        while(true)
        {
          line = r.ReadLine();
          if (line != null)
          {
            FileLines.Add(line);
          }
          else
          {
            break;
          }
        }
      }
  }
}