Reputation: 478
How to Get selected image name from gallery in xamarin android .when USer Click on button then image gallery is open and i get image but i don not know how to get the image name.
This is the button click event when user click on the button the image gallery is open and i select the image
fab2.Click += (o, e) =>
{
Intent = new Intent(Intent.ActionPick,
MediaStore.Images.Media.InternalContentUri);
Intent.SetType("image/*");
StartActivityForResult(Intent.CreateChooser(Intent,"SelectPicture"), 1 );
};
When image is selected i get the image but ...here is i have problem .i dont know how i get the selected image name.
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
Android.Net.Uri uri = data.Data;
string path = uri.Path;
string filename = path.Substring(path.LastIndexOf("/") + 1);
// String s = path(selectedImageUri);
Bitmap bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, data.Data);
}
}
Upvotes: 3
Views: 1502
Reputation: 478
Modify This Code Add Method Getpath() and it will return you path of selected image where you can get the Name of selected image.
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
string imagePath = null;
Android.Net.Uri uri = data.Data;
var path = GetPath(uri);
string filename = path.Substring(path.LastIndexOf("/") + 1);
// String s = path(selectedImageUri);
Bitmap bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, data.Data);
}
}
public string GetPath(Android.Net.Uri uri)
{
string path = null;
String[] projection = { MediaStore.MediaColumns.Data };
ContentResolver cr = ApplicationContext.ContentResolver;
var metaCursor = cr.Query(uri, projection, null, null, null);
if (metaCursor != null)
{
try
{
if (metaCursor.MoveToFirst())
{
path = metaCursor.GetString(0);
}
}
finally
{
metaCursor.Close();
}
}
return path;
}
Upvotes: 3