【问题标题】:How I can enable user to select image from gallery using xamarin media plugin and rename the image如何使用户能够使用 xamarin 媒体插件从图库中选择图像并重命名图像
【发布时间】:2019-01-17 09:34:44
【问题描述】:

我正在使用 xamarin 媒体插件;允许用户选择图像并重命名图像。

用户可以在使用相机拍摄时重命名照片。但是,当使用 xamarin 媒体插件从图库中选择时,用户如何重命名图像。

我有以下代码允许用户从图库中选择图像。当用户选择图像时;我将它保存在“文件”变量中。

我想重命名文件并将其保留在同一位置。

    private async void Select_From_Gallery_Button_Clicked(object sender, EventArgs e)
    {
        await CrossMedia.Current.Initialize();
        if (!CrossMedia.Current.IsPickPhotoSupported)
        {
            await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
            return;
        }

        Stream stream = null;

        var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
        {



        });
//Display the path
await DisplayAlert("yes", file.Path, "OK");

我可以看到文件的完整路径。我想从路径中获取名称并将图像重命名为不同的名称

【问题讨论】:

    标签: c# xamarin.forms


    【解决方案1】:

    您最好的选择就是使用不同的名称复制文件。

    首先创建您的界面(在您的 Xamarin.Forms 项目中)

    public interface IFile {
        void Copy ( string fromFile, string toFile );
    }
    

    然后,在各个平台实现服务。

    安卓:

    [assembly: Xamarin.Forms.Dependency (typeof (FileImplementation))]
    namespace File.Droid {
      public class FileImplementation : IFile
      {
          public FileImplementation() {}
    
          public void Copy(string fromFile, string toFile)
          {
                System.IO.File.Copy(fromFile, toFile);
          }
    
      }
    }
    

    iOS

    [assembly: Xamarin.Forms.Dependency (typeof (FileImplementation))]
    namespace File.iOS {
      public class FileImplementation : IFile
      {
          public FileImplementation() {}
    
          public void Copy(string fromFile, string toFile)
          {
                System.IO.File.Copy(fromFile, toFile);
          }
    
      }
    }
    

    然后,在您的代码调用中:

    var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
            {
    
    
    
            });
    DependencyService.Get<IFile>().Copy(file.Path, "YourName.png");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-18
      • 2018-01-17
      • 2016-12-30
      • 2020-12-22
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多