简体   繁体   English

如何从Xamarin Android中的图库中获取选定的图像名称

[英]How to get selected image name from gallery in xamarin android

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. 如何从xamarin android中的画廊中获取选定的图像名称。当用户单击按钮时,图像画廊打开,我得到图像,但我不知道如何获取图像名称。

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);
      }
    }

Modify This Code Add Method Getpath() and it will return you path of selected image where you can get the Name of selected image. 修改此代码Add Method Getpath(),它将返回您选择的图像的路径,您可以在其中获取选择的图像的名称。

 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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM