Hello! The problem seems to be standard, but nothing good comes out of the Internet examples. Task: on a mobile application, select a picture from the phone and send restSharp to the server using restSharp. RestSharp asks for the path to the file, but does not find the address that gives Uri "content: //com.android.providers.media.documents/document/image: 18", so probably a straight path is needed. Here's how to get it? From below, I gave a typical example that returns null for me, but Uri itself is correct, since the image is completely inserted into the imageView. Maybe someone solved such a problem of transferring the restSharp file.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (resultCode == Result.Ok) { var imageView = FindViewById<ImageView>(Resource.Id.editAvatarImageView); imageView.SetImageURI(data.Data); selectedAvatar = data.Data; string path = null; string[] projection = new[] { Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data }; using (ICursor cursor = ContentResolver.Query(data.Data, projection, null, null, null)) { if (cursor != null) { int columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data); cursor.MoveToFirst(); path = cursor.GetString(columnIndex); } } } } 
  • And if you read it through getContentResolver().openInputStream(uri) is available? - Eugene Krivenja

1 answer 1

Here is a working version, after digging for 3 days in the depths of the Internet

 protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); if (resultCode == Result.Ok) { var imageView = FindViewById<ImageView>(Resource.Id.editAvatarImageView); imageView.SetImageURI(data.Data); // selectedAvatar = data.Data; string doc_id = ""; using (var c1 = ContentResolver.Query(data.Data, null, null, null, null)) { c1.MoveToFirst(); String document_id = c1.GetString(0); doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1); } string path = null; // The projection contains the columns we want to return in our query. string selection = Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " =? "; using (var cursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] { doc_id }, null)) { var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data); cursor.MoveToFirst(); path = cursor.GetString(columnIndex); } imgPath = path; } } 
  • in fact ... this bucket works this way :)). Those. depends on the version of the bucket, where this code is working, where it is not. There are such bugs on the side of the bucket just the sea .. - CSharpUser
  • Well, on Jili Bean and Lolipop works the same way) then we'll see. In principle, the framework may allow to solve some trivia. - Dimka
  • then this code is not working for API 19 <. You can check. Therefore, you need to manually check the level of the API and accordingly add the code for the older version. - CSharpUser