0

I use a button to find a file in the phone:

    private void button_save_Click(object sender, System.EventArgs e)
    {
        Intent chooseFile = new Intent(Intent.ActionGetContent);
        chooseFile.SetType("*/*");
        chooseFile = Intent.CreateChooser(chooseFile, "Choose a file");
        StartActivityForResult(chooseFile, 1000);
    }

And then I try to use the given path to open the selected file:

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode is 1000 && resultCode is Result.Ok)
        {
            var uri = data.Data;
            string path = uri.Path;
            Java.IO.File file = new Java.IO.File(path);

        }
    }

but the format of uri.Path is incompatible with Java.IO.File and the file is not opened. How can I get a compatible full path to a file?

Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
  • Please tell the value of uri.Path and uri.String. And make clear why it is not compatible according to you. – blackapps Nov 18 '20 at 12:15
  • `How can I get a compatible full path to a file?` You will not do such nasty things as you should use that uri to handle the file. Even if you were able to convert the uri to a path then you would not have access under Android 10. Well mostly not. – blackapps Nov 18 '20 at 12:17
  • @blackapps uri.Path is "/document/primary:Telegram/Telegram Images/00.jpg" and uri.String is undefined – Minimus Heximus Nov 18 '20 at 12:35
  • Can be undefined as i dont know xamarin. I wanted to know the full uri. The full content scheme. There will be another member that will tell you. And you us. – blackapps Nov 18 '20 at 12:40
  • "uri = {content://com.android.externalstorage.documents/document/primary%3ATelegram%2FTelegram%20Images%2F00.jpg}" – Minimus Heximus Nov 18 '20 at 13:05
  • @blackapps to be comptible the path must be "/sdcard/Telegram/Telegram Images/00.jpg" instead. – Minimus Heximus Nov 18 '20 at 13:21
  • Well not too difficult to convert that scheme to your wanted path. But why would you need a path? You can do all with that uri. – blackapps Nov 18 '20 at 14:34
  • @blackapps There are a lot of File classes in C#. Are they compatible with Uri? How do you use Uri? – Minimus Heximus Nov 18 '20 at 15:05
  • Well the question is if you can open a stream for that content scheme in C#. I dont know .. C#. – blackapps Nov 18 '20 at 15:14
  • If you want to get the full path url from a content url, you can have a look at [this answer](https://stackoverflow.com/a/47008332/10539446). – nevermore Nov 19 '20 at 07:02

0 Answers0