2

I'm trying to get the path of a any kind of file but in don't know why it only works with images, with any other file extension path is always null. Here's my code:

private string GetPath(Android.Net.Uri uri)
    {
        string path;
        string[] projection = new[] { Android.Provider.MediaStore.MediaColumns.Data };
        using (ICursor cursor = ManagedQuery(uri, projection, null, null, null))
        {
            if (cursor != null)
            {
                int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.MediaColumns.Data);
                cursor.MoveToFirst();
                path = cursor.GetString(columnIndex);                    
            }
        }
        return path;
    }

Am I doing something wrong or is there any other way to do it?

Thank you!

Pau
  • 69
  • 1
  • 7

2 Answers2

1

Uri has a function getPath() which is called like this:

uri.getPath()

You can probably cast that to a string.

As suggested by this post:

Convert file: Uri to File in Android

EDIT

In Xamarin this function doesn't exist.

According to this post it should be quite easy:

Android File Path (Xamarin)

It uses the official docs of Xamarin:

http://developer.xamarin.com/recipes/android/other_ux/pick_image/

Community
  • 1
  • 1
Baklap4
  • 3,914
  • 2
  • 29
  • 56
  • Thanks for your answers :) But I was wondering if is there any way to do a generic function for all file extensions, not only with specific types (Android.Provider.MediaStore.Audio.Media.InterfaceConsts.Data only works for audio files, and Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data with image files) – Pau Dec 08 '14 at 19:20
  • If you want to get all the files i suggest this page: http://developer.xamarin.com/recipes/android/data/files/browse_files/ this will list every file in a specific directory. – Baklap4 Dec 08 '14 at 19:28
0

Thanks to this genius ryupold, there is a solution

https://gist.github.com/ryupold/fe38e5acbe1586681e27

im_not_josh
  • 483
  • 3
  • 10