2015-12-02 200 views
1

我使用下面的代碼到Android.Net.Uri路徑物理路徑轉換:如何將Android.Net.Uri路徑轉換爲xamarin中的物理路徑?

 private string GetPathToImage (Android.Net.Uri uri) 
    { 
     string path = null; 
     // The projection contains the columns we want to return in our query. 
     string[] projection = new[] { Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data }; 

     using (ICursor cursor = ManagedQuery (uri, projection, null, null, null)) { 
      if (cursor != null) { 
       int columnIndex = cursor.GetColumnIndexOrThrow (Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data); 
       cursor.MoveToFirst(); 
       path = cursor.GetString (columnIndex); 

      } 
     } 
     return path; 
    } 

但它不工作。我在「路徑」中獲得了空值。該Android.Net.Uri路徑如下:

//com.android.providers.media.documents/document/image%3A43306 

而且我要像肌肉發展路徑:

files/Pictures/temp/IMG_20151201_194231.jpg 

我怎樣才能做到這一點?

問候, 阿南德杜貝

+0

你找到答案! –

回答

0

嘗試類似的措施:

public String getRealPathFromURI(Context context, Uri contentUri) { 
    Cursor cursor = null; 
    try { 
    String[] proj = { MediaStore.Images.Media.DATA }; 
    cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    return cursor.getString(column_index); 
    } finally { 
    if (cursor != null) { 
     cursor.close(); 
    } 
    } 
} 

參見:https://stackoverflow.com/a/3414749/3891036

+0

感謝您的回覆,但仍收到空值。什麼可能是獲得空值的原因? – anand

+0

這將返回null –