3
\$\begingroup\$

I am trying to read one file from my android sd card from my unity application, but its not reading.

using the asset bundle i have exported my file and kept in my android device sd card and i am trying to fetch it and show into my scene.

Here is the code i have used for this:

public string URL;
public string URLAndroid = Application.persistentDataPath+/cube.unity3d;
public string AssetName;

private string path;

IEnumerator Start() {

    //path = System.IO.Path.Combine (Application.persistentDataPath, "/cube.unity3d");

    // Download the file from the URL. It will not be saved in the Cache
    using (WWW www = WWW.LoadFromCacheOrDownload(URLAndroid, 1)) {
        yield return www;

        if (www.error != null)
            throw new Exception("WWW download had an error:" + www.error);

        AssetBundle bundle = www.assetBundle;

        if (AssetName == "")
            Instantiate(bundle.mainAsset);
        else
            Instantiate(bundle.LoadAsset(AssetName));

        // Unload the AssetBundles compressed contents to conserve memory
        bundle.Unload(false);

    } // memory is freed from the web stream (www.Dispose() gets called implicitly)
}

Here i have used the URLAndroid as a normal downloading method and ihave tried to fethch the model using system.io method but its not happening. I have created the corresponding folder in my android device and kept my assets eventhough its not happening.

Plesae help me to solve this issue.

\$\endgroup\$
  • \$\begingroup\$ I have tried this all option but no use: path = "jar:file://" + Application.dataPath + "!/assets/cube.unity3d"; path = Application.persistentDataPath+"/cube.unity3d"; path = "/mnt/sdcard/cube.unity3d"; path = "/sdcard/cube.unity3d"; path = "/storage/sdcard0/cube.unity3d"; path = "/storage/sdcard0/Android/data/com.gh.Testing/files/cube.unity3d"; \$\endgroup\$ – Sivamurugan Oct 10 '15 at 7:20
1
\$\begingroup\$

To solve this problem I created pre-compilation script which will run before the compilation; this script will list the names of the files you want to access later in text file

 #if UNITY_EDITOR
 using UnityEditor.Build;
 using UnityEditor;
 using System.IO;

 public class BM_AndroidBuildPrepartion : IPreprocessBuild
 {
     public int callbackOrder { get { return 0; } }
     public void OnPreprocessBuild(BuildTarget target, string path)
     {
         // Do the preprocessing here
         string[] fileEntries = Directory.GetFiles("Assets/Resources/Prefabs/alphabet", "*.prefab");
         System.IO.Directory.CreateDirectory("Assets/StreamingAssets/");
         using (StreamWriter sw = new StreamWriter("Assets/StreamingAssets/alphabet.txt", false))
         {

         foreach (string filename in fileEntries) {
                 sw.WriteLine(Path.GetFileNameWithoutExtension(filename));
         }

         }
     }
 }
 #endif

then I read the text file and you can access the files in same way but you have to put them inside your project in Assets/StreamingAssets/

   #if UNITY_ANDROID
        string  path = "jar:file://" + Application.dataPath + "!/assets/alphabet.txt";
          WWW wwwfile = new WWW(path);
          while (!wwwfile.isDone) { }
          var filepath = string.Format("{0}/{1}", Application.persistentDataPath, "alphabet.t");
          File.WriteAllBytes(filepath, wwwfile.bytes);

          StreamReader wr = new StreamReader(filepath);
              string line;
              while ((line = wr.ReadLine()) != null)
              {
              //your code
              }
   #endif
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.