Search code examples
deploymentassetshololens

How to access files in your assets folder from a deployed HoloLens app?


So i have a couple of files i need to use in my application. Both are NLP related .nbin files. I stored them directly in my assets folder without using any sub directories in it. (.../Assets/EnglishTOK.nbin)

Now i'm using Application.DataPath to get access to assets folder without specifying the absolute path on my PC (Working). But after i deploy it to the HoloLens, i'm getting the following error.

FileNotFoundException C:\Data\Users\DefaultAccount\AppData\Local\DevelopmentFiles\Template3DVS.Release_Win32.Dinesh\Data\EnglishTok.nbin

and the above path is shown. Being a beginner, I am not able to debug the problem.

1)How do i gain access to those files?

2)If this process is incorrect, is there any other way on how to include files in my app? (Not creating new but using existing ones)

3)If i do upload files from Windows Device Portal, where should i place those files? How do i access them? Any new Paths needs to be specified?

Thanks for your time.!

Edit : 1. To use video/audio/image files in your device, use Assets/StreamingAssets folder to place those files. Use Application.streamingAssetsPath to get the path to that folder.

  1. To use Assets folder, the answer given works fine.

Solution

  • You'll want to create a folder named Resources inside your Assets folder, and place your files in there. You then load your file using something like:

    TextAsset asset = Resources.Load("myfile.bytes"); 
    

    You may want to change the file extension into one that Unity recognizes, for instance '.bytes' or '.txt', to avoid serialization issues. For more info on loading resources, see https://docs.unity3d.com/560/Documentation/Manual/LoadingResourcesatRuntime.html

    For your last question, you can access files from the Windows Device Portal, but they should then be placed in User Folders \ LocalAppData \ your-app \ LocalState \

    They can then be retrieved by loading the content as a file stream:

    filename = "yourfile.bytes";
    string streamstring = null;
    if (File.Exists(Application.persistentDataPath + "/" + filename))
    {
        FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open);
        byte [] bytes = new byte [fs.Length];
        fs.Read(bytes, 0, (int) fs.Length);
        streamstring = Encoding.UTF8.GetString(bytes);
        fs.Dispose();
    }