use filepicker to select a text file and load the content of that text file into a text editing app in xamarin android

After you select file,you will get the data in the OnActivityResult method,you could try below method:

class Notes:AppCompatActivity
//Declare this edit text variable in the class so that all methods can access it without having to redefine it
   EditText notes;
    protected override void OnCreate(Bundle onSavedInstanceState){
       //Assign the edit text
         notes = this.FindViewById<EditText>(Resource.Id.editext5);
//This button method opens the file picker interface when the button is clicked
   private void Button2_Click(object sender, EventArgs e)

        //Program the open file text behavior from here
        Intent intent = new Intent();
        StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);

   protected override async void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Result.Ok)
            var uri = data.Data;
            var stream = ContentResolver.OpenInputStream(uri);

            string str = "";
            StringBuffer buf = new StringBuffer();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            while ((str = reader.ReadLine()) != null)
                buf.Append(str + "\n");
            notes.Text = buf.ToString();



CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top