使用 filepicker select 文本文件并将该文本文件的内容加载到 Xamarin 中的编辑文本 Android

Use filepicker to select a text file and load the content of that text file into an Edit Text in Xamarin Android

在这个论坛上问了一个相关问题后,我很感激找到了打开文件选择器界面的代码,使用户能够选择 mime 类型 (.txt) 的文件,不用我需要提前过去并将该文本文件的内容加载到我的 activity 布局上的编辑文本中... 打开文件选择器界面的代码位于按钮方法中...长话短说,我的其余代码在下面解释。

 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();
            intent.SetType("text/plain");
            intent.SetAction(Intent.ActionGetContent);
            StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 1);
        }
    //I had this idea to override StartActivityForResult method but am not even sure that is what i should do
         public override void StartActivityForResult(Intent intent, int requestCode)
        {
            base.StartActivityForResult(intent, requestCode);
        }
     }

将帮助我将所选文件中现有的文本加载到我的编辑文本中的代码肯定会受到赞赏,谢谢...

你select文件后,你会在OnActivityResult方法中得到数据,你可以试试下面的方法:

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();
        intent.SetType("text/plain");
        intent.SetAction(Intent.ActionGetContent);
        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");
            }
            stream.Close();
            notes.Text = buf.ToString();

        }

    }
 }