Find files on Xamarin Android

1

I'm putting an xml file inside the project but it is not being located by my XmlTextReader where in the project should I put this file and how can I find it in the emulator folder?

I'm using Xamarin Android.

    
asked by anonymous 07.08.2014 / 16:29

1 answer

1

You can not just add a file anywhere in this way.

Unlike Windows where you can include files in a project and have them copied to the program folder, in Android the program should contain everything you need in a single file, Apk, any file that is outside Apk it is your program's obligation to know how to download from the appropriate location.

In your case what can be done is to move this file to the Assets folder, it is in this folder that there should be any file that your program needs to use, and the Build Action in this file should be as "AndroidAsset", this way including the file inside Apk.

If you open the AboutAssets.txt file that comes by default in the project inside the Assets folder you will find the following example of how to access this file

public class ReadAsset : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        InputStream input = Assets.Open ("my_asset.txt");
    }
}

The only detail is that assets are read-only, if you need this file to be changed then you must include the default file in this folder and save the changes somewhere else, so it's up to your program every time you read the file first check if you already have a saved file, if you have not extracted the assets.

There is also a limitation on older versions of Android, if I am not mistaken from 2.3 down but I do not remember the exact version, in which it can not read files with more than 1mb in Assets, I believe it will not be yours problem, but if it happens the only solution I found was to break the file into several 1mb files and then extract and merge all back.

    
08.08.2014 / 15:21