0

I have the following code to browse and choose a file from android file system

Intent intent = new Intent(Intent.ActionGetContent);
intent.SetType("*/*");
intent.AddCategory(Intent.CategoryOpenable);
StartActivityForResult(Intent.CreateChooser(intent, "Select a file"),0);

Now I want to get the content of the choosen file. I read that , to do that I have to put some code in

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
}

but this code never gets called and from the examples given I don't understand very good what to put in there. The file I want to read is a CSV file abd I want to put its data in an Array.

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
proffstack
  • 45
  • 9
  • Modify your code to : `protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)`. – Grace Feng Nov 10 '17 at 13:32
  • thank you. now the debug enters into the method. But how to get the file content. I'm using C# (android). All the answers I found seems to work for Java. – proffstack Nov 10 '17 at 14:35

1 Answers1

0

You could get the file's path when you choose a file from android file system, for example :

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (requestCode == 0)
    {
        var uri = data.Data;
        string path = GetActualPathFromFile(uri);
        System.Diagnostics.Debug.WriteLine("File path == " + path);        
    }
}

The GetActualPathFromFile(uri) method is to get the actual path from Android.Net.Uri, for complete code, you could refer to : How to get actual path from Uri xamarin android .

After get the file path, you could get your CSV file content by refer to this : Reading CSV files using C# .

Grace Feng
  • 16,564
  • 2
  • 22
  • 45