Using StreamingAssets for Android Build

Hello, I’m creating a Unity build for Windows & Android platforms that utilizes the StreamingAssets folder. I both read and write into this file, using code spinets such as:

string fileText = File.ReadAllText(Application.streamingAssetsPath + "/dialogue/exampleName.txt", Encoding.UTF8)
File.AppendAllText(Application.streamingAssetsPath + "/dialogue/exampleName.txt", fileData, Encoding.UTF8)

This works fine for Windows, however, when using ‘Android Logcat’ to debug my Android emulator, I can see that this process isn’t working for Android. Below is an example of an error I received:

Error Unity DirectoryNotFoundException: Could not find a part of the path "/jar:file:/data/app/~~cgnOTp1Hnbf79qkniGQQaA==/com.DefaultCompany.Game-Mrx_8jC0_G1ZWPVghbDmhg==/base.apk!/assets/dialogue/exampleName/textFile.txt".

When trying to fix this issue, I saw some advise about using ‘UnityWebRequest’ to change the format of the file path string when accessing the Android StreamingAssets folder, but I’m a little confused about how to go about doing this, in all honesty. If possible, can someone explain how to edit my code in such a way so that the following is accomplished:

string fileText = File.ReadAllText(androidPath(Application.streamingAssetsPath + "/dialogue/exampleName.txt"), Encoding.UTF8)
File.AppendAllText(androidPath(Application.streamingAssetsPath + "/dialogue/exampleName.txt"), fileData, Encoding.UTF8)

private string androidPath(string filePath)
{
// WebRequest code here changes filePath to 'androidPath' proper format
    return androidPath
}

If this isn’t possible, can someone instead explain what I am misunderstanding about this process for Android devices? Such as, if reading / writing into StreamingAssets isn’t possible, or if it’s not as simple as changing the format of the file path, or something else? Thank you.

It’s not that you use UnityWebRequest to somehow get a different path. It’s that you do not use file API’s at all in order to access these on Android. It’s explained in the documentation here.https://docs.unity3d.com/6000.0/Documentation/Manual/StreamingAssets.html

As stated in those docs: “ On Android and the Web platform, it’s not possible to access the streaming asset files directly via file system APIs and streamingAssets path because these platforms return a URL. Use the UnityWebRequest class to access the content instead.”

On Android this is because your streaming assets are compressed inside of the APK on device.

So you need to use UnityWebRequest to get your files from the URL base provided by those docs on Android.

(Just as you would use it in order to access a local file at a file:// url, for example).