This is part of my program in C#(Win):
C#
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:
C#
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

Tasadduq BurneyRank #1,427 · 1.1K reputationPosted Sep 1, 2023, 4:58 PM EDT
To open a file in Android using C#, you can use the Android API to handle file selection and then read the contents of the selected file. Here's a C# code snippet that demonstrates how to do this:
using System; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Widget; using System.IO; namespace YourNamespace { [Activity(Label = "OpenFileActivity")] public class OpenFileActivity : Activity { private const int FilePickerRequestCode = 1; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_open_file); Button openFileButton = FindViewById(Resource.Id.openFileButton); openFileButton.Click += OpenFileButton_Click; } private void OpenFileButton_Click(object sender, EventArgs e) { Intent intent = new Intent(Intent.ActionOpenDocument); intent.AddCategory(Intent.CategoryOpenable); intent.SetType("*/*"); // Set the MIME type to allow opening all file types StartActivityForResult(intent, FilePickerRequestCode); } protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (requestCode == FilePickerRequestCode && resultCode == Result.Ok && data != null) { Android.Net.Uri uri = data.Data; string filePath = GetPathFromUri(uri); if (!string.IsNullOrEmpty(filePath)) { // Read the file content string fileData = File.ReadAllText(filePath); // Now you have the file data in the 'fileData' variable // You can process it further as needed } } } private string GetPathFromUri(Android.Net.Uri uri) { string[] projection = { Android.Provider.MediaStore.Images.ImageColumns.Data }; ContentResolver cr = ContentResolver; using (ICursor cursor = cr.Query(uri, projection, null, null, null)) { if (cursor != null && cursor.MoveToFirst()) { int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.ImageColumns.Data); return cursor.GetString(columnIndex); } } return null; } } }Ronald Gans12 reputationPosted Sep 5, 2023, 1:12 PM EDT
Here's my code:
try
{
var intent = new Intent(Intent.ActionOpenDocumentTree);
intent.PutExtra("android.content.extra.SHOW_ADVANCED", true);
intent.PutExtra("android.content.extra.FANCY", true);
intent.PutExtra("android.content.extra.SHOW_FILESIZE", true);
intent.SetType("*/*"); // Set the MIME type to allow opening all file types
StartActivityForResult(intent, FilePickerRequestCode);
and the error I get is: "Implicit evaluation is disabled"
Ronald Gans12 reputationPosted Sep 7, 2023, 9:30 AM EDT
Now when I use this code:
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"), 1)
I can see the file system (Documents) and if I press and hold on one of the file icons, it changes color, I assume to show it is selected. And, infrequently, when I click on the selected image (of a file; these are text files) I get another screen asking me what app to use to open the file and that works. But not always. I'm not sure what to do from here. Any help is greatly appreciated.