简体   繁体   English

如何使用Xamarin媒体插件使用户能够从画廊中选择图像并重命名图像

[英]How I can enable user to select image from gallery using xamarin media plugin and rename the image

I'm using xamarin media plugin; 我正在使用xamarin媒体插件; allowing user to select the image and rename the image. 允许用户选择图像并重命名图像。

Users are able to rename the photo when they take with camera. 用户使用相机拍摄时可以重命名照片。 But how user can rename the image when when select from gallery using the xamarin media plugin. 但是当使用xamarin媒体插件从图库中选择时,用户如何重命名图像。

I have below code to allow user select the image from the gallery. 我有以下代码,允许用户从图库中选择图像。 When user select image; 当用户选择图片时; I'm saving it in 'file' variable. 我将其保存在“文件”变量中。

I would like to rename the file and keep it in same location. 我想重命名该文件并将其保留在相同的位置。

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

I can see the full path of file. 我可以看到文件的完整路径。 I would like to get name out of path and rename the image to different name 我想从路径中获取名称并将图像重命名为其他名称

Your best option is simply to make a copy of the file with a different name. 最好的选择就是简单地使用其他名称复制文件。

First create your interface (in your Xamarin.Forms project) 首先创建您的界面(在您的Xamarin.Forms项目中)

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

Then, implement the services in each platform. 然后,在每个平台中实施服务。

Android: 安卓:

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

  }
}

Then, in your code call: 然后,在您的代码调用中:

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



        });
DependencyService.Get<IFile>().Copy(file.Path, "YourName.png");

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

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