如何在Xamarin Android上的任何选择文件上获取文件路径

时间:2018-04-26 11:46:32

标签: android xamarin

如何从android上的本地或外部存储获取的任何文件中获取路径?这是我的代码:

                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);

我得到的OnActivityResults:

         base.OnActivityResult(requestCode, resultCode, data);

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

        }

FilePath在' document / 3137-3837:Screenshots / Screenshot_2018.png 之间有一个冒号。如何在没有冒号的情况下获得有效路径?

1 个答案:

答案 0 :(得分:0)

您在此处检索的文件路径:

var FilePath= data.Data.Path; 

只是一个URI,您需要将其转换为实际的文件路径,但由于您要返回varius MIME类型,因此需要以不同方式处理URI,

This updated answer from a post should help