I’m getting an access denied exception when opening a file stream on a file placed in StreamingAssets.
Everything’s hunky dory in the editor and in desktop builds, but on iOS, nopey. The path seems correct, the file exists, but no access…
Example code:
using System.IO;
public class StreamingAssetsTest : Monobehaviour
{
public string relativePath;
void Start()
{
string fullPath = Path.combine( Application.streamingAssetsPath, relativePath );
if( File.Exists( fullPath )
{
using( FileStream fs = new FileStream( fullPath, FileMode.Open ) ) // Nopey. Access denied...
{
}
}
}
}
I found other users posting about similar issues in 2013, with no solution in sight.
Which seems to apply to Android and web player, but who knows?
One solution would be to include files I need access to in the Xcode bundle and call [ NSBundle pathForResource… ] to retrieve the path in managed code. But that very much breaks the multi-platform workflow Unity is great for.
Your problem is that you need to use File.OpenRead, FileMode.Open has read/write capabilities and that is prohibited by iOS unless you are using a jailbroken device, use the following:
myStream = File.OpenRead(myPath); //Read only access…
I’m super late to this party, but I wanted to add that I was getting a similar issue running the game on some mac/windows machines, and the OpenRead() was what seems to have fixed it. Really glad I found this thread!
I had a similar problem recently and I managed to read file on iOS from StreamingAssets using File.Open and specifying the FileAccess to Read like this
using (FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read))
{
}