Ronald Gans

Ronald Gans

  • 1.6k
  • 12
  • 5.4k

How to open a file on Android using C#

Sep 1 2023 3:36 PM

This is part of my program in C#(Win):

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title ="Open File";
try
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string file = openFileDialog1.FileName;
        string fileData = File.ReadAllBytes(file);
    }
}

But in Android, the closest I can come is this:

Intent intent = new Intent(Intent.ActionGetContent);
intent.SetType("text/plain");               
intent.AddCategory(Intent.CategoryOpenable);
intent.SetAction(Intent.ActionGetContent);
intent.PutExtra("android.content.extra.SHOW_ADVANCED", true);
intent.PutExtra("android.content.extra.FANCY", true);
intent.PutExtra("android.content.extra.SHOW_FILESIZE", true);
StartActivityForResult(Intent.CreateChooser(intent, "Select file"), 0);

And while I can look at the files in various folders, I cannot open any of them.

Thanks. RON


Answers (3)