Forum Xamarin.Android

Using Android Assets to copy stuff to external data directories

GordonPaganGordonPagan GBMember
edited October 2012 in Xamarin.Android

For reasons of compatibility I find I have to use Mono for Android on Visual Studio 2010. I have the same development on MonoDevelop however that has different problems which I will report in a separate discussion.

The issue I have is how to piggy-back essential data files which form the kernel of the application deliverables but do not form part of the code structure itself. On iOS I found it easy in Mono to add in datafiles to the Application then copy them across to a user area where the user could read and write the files at leisure. It seems to me that there is only one way to piggy back data code in an Android App and that is using the Asset directory with the included files marked up to be 'AndroidAsset' under Build Action. There is even an example in the OpenGL implementation that I am using called 'AboutAssets.txt' which (initially) I found helpful. Here is a snippet of code I use to trial this out on the OnCreate part of the start of the main Activity:-

                    public static bool first_setup()

                        var fname = "AboutAssets.txt";
                            var assets=android_context.Assets.List("");
                            diag("Assets available: " + assets.Length.ToString());
                            for (int i = 0; i < assets.Length; i++)
                            var input=android_context.Assets.Open(fname);
                            diag("So far so good");
                            var size = input.Length;
                        catch (Exception e)
                            diag("Exception opening asset " + fname + " exception: " + e.Message);
                    return false;
                        return true;

The routine 'diag' is just a text diagnostic that I have to log lines in OpenGL applications. Here is the output I record in 'diag':-

__Assets available: 5






So far so good

Exception opening asset AboutAssets.txt exception: Operation is not supported._

As you can see it lists the assets and opens the stream ok. However if you try reading from it, it is not too happy.

If there is any advice or correction to my possible incorrect assertions here, I would be most happy to hear from any interested parties.


Sign In or Register to comment.