Quantcast
Channel: How to get actual path from Uri xamarin android - Stack Overflow
Viewing all articles
Browse latest Browse all 5

How to get actual path from Uri xamarin android

$
0
0

I want to get actual path from Uri, when I select file using intent then it will be returning URI but below line of code not working for conversion URI to string path

Open FilePicker

public static void PickFile(Activity context){    Intent intent = new Intent(Intent.ActionGetContent);    intent.SetType("*/*");    context.StartActivityForResult(intent, PICKFILE_RESULT_CODE);}

Get Result

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data){    if(requestCode == PICKFILE_RESULT_CODE)    {        string filepath = GetRealPathFromURI(MainActivity.act, data.Data);    } }public static string GetRealPathFromURI(Activity act, Android.Net.Uri contentURI){    try    {        ICursor cursor = act.ContentResolver.Query(contentURI, null, null, null, null);        cursor.MoveToFirst();        string documentId = cursor.GetString(0);        documentId = documentId.Split(':')[1];        cursor.Close();        cursor = act.ContentResolver.Query(        MediaStore.Images.Media.ExternalContentUri,        null, MediaStore.Images.Media.InterfaceConsts.Id +" = ? ", new[] { documentId }, null);        cursor.MoveToFirst();        string path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));        cursor.Close();        return path;    }    catch(Exception e)    {        Log.Debug("TAG_DATA",e.ToString());        return "";    }}

Error

System.IndexOutOfRangeException: Index was outside the bounds of the array.

can anyone explain to me, what's wrong think I am doing in this code.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images