In the game I am developing, I have created a serializable class that generates a .map file for saving and loading levels. In XNA, this works great using the following code:

FileStream fileStream = new FileStream(filePath,
BinaryFormatter formatter = new BinaryFormatter();
MapInfo data = (MapInfo)formatter.Deserialize(fileStream);

Unfortunately, in android it appears that the filePath ends up at the device's root (argument being passed in as "Content/maps"). After some research, I came across this page from xamarin that explains how to properly load assets from the asset folder. However, this action takes place in a separate namespace from my main activity, and so far have not found a way to access the Assets.Open() function.

any other ways to load that asset?

UPDATE: After playing around some more, I ended up trying to pass the Assets property of the main activity down through function calls, loading the file this way:

Stream fileStream = androidGame.Assets.Open(filePath);
BinaryFormatter formatter = new BinaryFormatter();
MapInfo data = (MapInfo)formatter.Deserialize(fileStream);

SADLY, the file still does not load. Looking at the variables in debug shows the correct address for the activity contained in the variable androidGame, but says Open is an "Unknown member"


In case anyone comes across file loading issues in Monogame Android, this is what I ended up doing:

First, make sure you copy/reference your files in the correct dir of your android build. This should appear somewhere under Assets/Content/yourfile.file or subfolder.

Then, check the properties of the file and select "AndroidAsset" for build options and "Copy if newer" for output.

Finally, open the stream using the Assets property of your main activity. This can be accessed using your Microsoft.Xna.Framework.Game class in this way:

Stream fileStream = Game.Activity.Assets.Open(path);

Turns out my issue was not the stream itself, but the deserialization which could not open a .dll in android (overlooked that one...). I ended up switching to XML serialization, since the binary serialization is build dependent

  • \$\begingroup\$ This should probably be on Stack Overflow (or in Xamarin's forums). There's nothing especially game-specific going on here. \$\endgroup\$ Jun 16, 2014 at 3:16
  • \$\begingroup\$ Alright, I will try somewhere else. Will post a workaround if I find a solution \$\endgroup\$ Jun 16, 2014 at 6:57
  • \$\begingroup\$ @AndrewRussell - it is tagged with MonoGame. Otherwise we need to migrate all Unity3d assets questions as well. \$\endgroup\$
    – Den
    Jun 16, 2014 at 9:26
  • \$\begingroup\$ @StickerAddicts - so are you using MonoGame or just plain Xamarin Android? \$\endgroup\$
    – Den
    Jun 16, 2014 at 9:26
  • 1
    \$\begingroup\$ @AndrewRussell There's nothing game specific about the code in the question, but the answer is actually very specific to MonoGame or XNA so I would say it is definitely posted in the correct place. \$\endgroup\$ Jun 23, 2014 at 6:31

1 Answer 1


I believe you can use TitleContainer.OpenStream in MonoGame for a platform independent way of reading and writing files.

See the XNA documentation for reference:


The thing you still have to be careful of is case sensitivity and formatting the path correctly. Otherwise I think it's relatively straightforward.

EDIT: Actually the XNA documentation is pretty sparse so here's an example that should give you the right idea. If your file is in the Content folder and you've set it to Content / Copy if newer in properties.

var filePath= Path.Combine(Content.RootDirectory, "FileName.dat");
using(var stream = TitleContainer.OpenStream(filePath))
    BinaryFormatter formatter = new BinaryFormatter();
    MapInfo data = (MapInfo)formatter.Deserialize(stream);

    // do your thing here
  • \$\begingroup\$ Thanks for the help, TitleContainer turned out to be handy. Although it did not actually solve this particular problem on the android version (monogame doesn't appear to have a complete implementation?), it simplified iOS and Windows builds \$\endgroup\$ Jun 26, 2014 at 22:56
  • \$\begingroup\$ What error are you getting? I've been using TitleContainer on Android for a while now. \$\endgroup\$ Jun 27, 2014 at 2:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .