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
Ronald GansPosted Sep 7, 2023, 1:30 PM
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.
Ronald GansPosted Sep 5, 2023, 5:12 PM
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"
Tasadduq BurneyPosted Sep 1, 2023, 8:58 PM
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: