0

How can I get the path from any file that I get from local or external storage on android? Here is my code:

                Intent = new Intent(Intent.ActionOpenDocument);
                string[] mimetypes =
                 {
                    "application/pdf", "image/png", "image/jpeg", "image/tiff", "application/msword",
                    "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                    "application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                };
                Intent.PutExtra(Intent.ExtraMimeTypes, mimetypes);
                Intent.SetType("*/*");

                Intent.SetAction(Intent.ActionGetContent);
                Intent.AddCategory(Intent.CategoryOpenable);

                StartActivityForResult(Intent.CreateChooser(Intent, "Select file"), Constant.PickPDFId);

And the OnActivityResults I got:

         base.OnActivityResult(requestCode, resultCode, data);

        if (requestCode == Constant.PickPDFId && resultCode == Result.Ok && data != null)
        {
            var FilePath= data.Data.Path;

        }

The FilePath has a colon in between 'document/3137-3837:Screenshots/Screenshot_2018.png . How can I get the valid path without the colon?

Jan Černý
  • 1,268
  • 2
  • 17
  • 31

1 Answers1

1

The Filepath you retrieved here:

var FilePath= data.Data.Path; 

is only a URI, you need to convert it to an actual file path, but because you are returning varius MIME types you need to process the URI differently,

This updated answer from a post should help

Tolulope
  • 488
  • 1
  • 5
  • 19