DirectoryNotFoundException: Could not find a part of the path

Hello! I’m having a problem trying to load an Streaming Asset on my Android game. It works neatly on the Editor, but not on the build. The error log is this one:

The code for loading the asset consists of two methods:

   public void LoadLocalizedText(string fileName){
       localizedText = new Dictionary<string, string> ();
       string filePath = Path.Combine (Application.streamingAssetsPath, fileName);

       string newPath = Path.Combine(Application.persistentDataPath, fileName);

       DoWWW_Android(fileName, filePath, newPath);

       if (File.Exists(newPath)) {
           string dataAsJson = File.ReadAllText(filePath);
           LocalizationData loadedData = JsonUtility.FromJson<LocalizationData>(dataAsJson);

           for (int i = 0; i < loadedData.items.Length; i++){
               localizedText.Add(loadedData.items[i].key, loadedData.items[i].value);
           }

           Debug.Log("Data loaded, dictionary contains " + localizedText.Count.ToString() + " entries");
       } else {
           Debug.LogError("Cannot Find Localization File!");
       }

       isReady = true;
   }

and

   static void DoWWW_Android(string name, string origPath, string newPath) {
       using (UnityWebRequest reader = UnityWebRequest.Get(origPath)) {
           reader.SendWebRequest();
           while (!reader.isDone) {
           }
           if (!reader.isHttpError && !reader.isNetworkError) {
               File.WriteAllBytes(newPath, reader.downloadHandler.data);
           }
           else {
               Debug.LogError("Database " + name + " not found at " + origPath);
               Debug.LogError("Error: " + reader.error);
           }
       }
   }

Can anyone help me to find what’s going wrong with this??

You can’t use File.ReadAllText as on line 10 of your script in Android. You must use UnityWebRequest.

Additionally, StreamingAssets cannot be written to on Android. It is read only.

See here: Unity - Manual: Streaming Assets

1 Like

Everything that Praetor says above, plus this is my directory finder since Unity for some inscrutible bizarre reason thinks we want to care about these nitty gritty dirty laundry details of paths on different platforms:

using UnityEngine;

public static class StreamingAssetsPath
{
    public static string StreamingAssetPathForWWW()
    {
        #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN
        return "file://" + Application.dataPath + "/StreamingAssets/";
        #endif
        #if UNITY_ANDROID
        return "jar:file://" + Application.dataPath + "!/assets/";
        #endif
        #if UNITY_IOS
        return "file://" + Application.dataPath + "/Raw/";
        #endif
        throw new System.NotImplementedException( "Check the ifdefs above.");
    }
    public static string StreamingAssetPathForFileOpen()
    {
        #if !UNITY_EDITOR && UNITY_ANDROID
        throw new System.NotImplementedException( "You cannot open files on Android. Must use WWW");
        #endif

        Debug.Log( "Application.streamingAssetsPath:" + Application.streamingAssetsPath);
        return Application.streamingAssetsPath + "/";
    }
}
4 Likes

Hi,
Can you show me an example of how to use UnityWebRequest to access a file in the android build

1 Like

an android example will be

string path = Path.Combine (Application.streamingAssetsPath, "fileName.txt");
var loadingRequest = UnityWebRequest.Get(path);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone && !loadingRequest.isNetworkError && !loadingRequest.isHttpError);
string result = System.Text.Encoding.UTF8.GetString(loadingRequest.downloadHandler.data);

hi, I want to load a unity.sentis ML model but getting this compilation error (Argument 1: cannot convert from ‘byte[ ]’ to ‘Unity.Sentis.ModelAsset’) while loading the download data.

void LoadModel()
    {
        //Load model
        const string modelName = "yolov8n.sentis";
        string modelPath = Path.Combine(Application.streamingAssetsPath, modelName);
        var loadingRequest = UnityWebRequest.Get(modelPath);
        loadingRequest.SendWebRequest();
        while (!loadingRequest.isDone && loadingRequest.result == UnityWebRequest.Result.ConnectionError && loadingRequest.result == UnityWebRequest.Result.ProtocolError);
      
        model = ModelLoader.Load(loadingRequest.downloadHandler.data);

    }
1 Like

which Unity version are you using?