Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Asset Files using VS & Monodroid

I've been trying to implement this example using C# and Monodroid, but am having difficulties reading and writing an Asset file:

http://docs.xamarin.com/android/advanced_topics/using_android_assets

I am using the emulator, not a device.

First of all, I am having trouble finding the namespace for Assets.Open. What I ultimately found was

const string lfn  = MyAssetFile.txt;
System.IO.StreamReader(Android.Content.Res.Resources.System.Assets.Open(lfn);

Is this the correct namespace?

Second of all, my Asset file is marked as AndroidAsset and "Copy Always" in the VS "Properties" pane, but my attempts to read the file always fail (File Not Found) using this statement:

string  settings = "";
using (StreamReader sr = new System.IO.StreamReader (Android.Content.Res.Resources.System.Assets.Open(lfn))) settings   =   sr.ReadToEnd();

Do I have my VS settings wrong so that the asset file not being copied onto the emulator, or is it being copied OK but my code to open/read it is wrong?

like image 813
The Mighty Skunk Avatar asked Mar 02 '12 20:03

The Mighty Skunk


People also ask

How do I access an asset file?

Step 3 – Right click app >> New >> Folder >> Assets folder. Right click on the assets folder, select New >> file (myText. txt) and your text.

How do I get an asset file on Android?

Step 1: To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder. Step 3: Android Studio will open a dialog box. Keep all the settings default.

What is the use of assets folder in Android?

Assets provide a way to include arbitrary files like text, xml, fonts, music, and video in your application. If you try to include these files as "resources", Android will process them into its resource system and you will not be able to get the raw data.

Where is assets folder in Android Studio?

In a typical Android Studio project, you will have an app/ module, with a main/ sourceset ( app/src/main/ off of the project root), and so your primary assets would go in app/src/main/assets/ .


1 Answers

You must use:

const string lfn = "MyAssetFile.txt";
string settings = string.Empty;

// context could be ApplicationContext, Activity or
// any other object of type Context
using (var input = context.Assets.Open(lfn))
using (StreamReader sr = new System.IO.StreamReader(input))
{
    settings = sr.ReadToEnd();
}

If I remember correctly, as I haven't got an IDE at the moment, Android.Content.Res.Resources.System.Assets references the Android assets not your project assets. You want to use your projects assets so you need to use the AssetManager from your Activities or Contexts.

For example: the Activity class has a property called Assets. This is what you use. OR you use a View's Context.Assets property.

like image 197
Matthew Avatar answered Oct 13 '22 23:10

Matthew