Find files on Xamarin Android

Asked

Viewed 689 times

1

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

I’m using Xamarin Android.

Soluçao

  • Where did you put this file in your project?

  • @Leandrogodoyrosa together with activitys

1 answer

1

You can’t just add a file anywhere in this way.

Other than 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 of Apk is obligatory for your program 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 should be any file that your program needs to use, and the Build Action (Build Action in Xamarin Studio) of this file should be as "Androidasset", thus including the file within 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 should include the default file in this folder and save the changes somewhere else, then it is up to your program to always read the file first to check if you already have a saved file, if you have not extracted from the Assets.

There’s also a limitation on older versions of Android, if I’m not mistaken about the 2.3 down but I can’t remember the exact version, where he can’t read files with more than 1mb in Assets, I don’t think that’s gonna be his problem, but if it happens the only solution I found was to break the file into several files of 1mb and then extract and join all back.

  • What I do, ta giving error C: Users Visit Documents Projects TB Table aapt.exe: Error: Invalid Resource directory name: "Assets". (Table)

  • When you select the file and look in the properties window check if Build Action (Build Action) is as Androidasset

Browser other questions tagged

You are not signed in. Login or sign up in order to post.