首页 > 解决方案 > 如何从从图库中挑选的图像中获取文件名?

问题描述

我正在尝试从库中获取文件名,但我正在使用的库似乎没有检索文件名的方法,但它具有检索文件路径的方法,我使用的是 Xam.Plugin.Media。我需要将文件名作为参数传递给我正在使用的其他方法。这是我的代码:

   var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new 
   Plugin.Media.Abstractions.PickMediaOptions
            {
                PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
           
            });


            if (file == null) return;
            imgChoosed.Source = ImageSource.FromStream(() => {
                var stream = file.GetStream();
                return stream;
            });

            // computerVision = new ComputerVisionViewModel();

           //I need to pass the file name as an argument here
           // var result = await computerVision.ConnectToVisualRecognition(file.Path,file.Name);

标签: c#mvvmxamarin.forms

解决方案


用这个。

var fileNameWithExtenstion = Path.GetFileName(filePath);
var fileNameWithoutExtenstion = Path.GetFileNameWithoutExtension(filePath);

推荐阅读