C# 如何使用xamarin媒体插件让用户从图库中选择图像并重命名图像
我正在使用xamarin媒体插件;允许用户选择图像并重命名图像 用户可以在使用相机拍照时重命名照片。但当用户使用xamarin媒体插件从gallery中选择时,如何重命名图像 我有下面的代码允许用户从图库中选择图像。当用户选择图像时;我将它保存在'file'变量中 我想重命名该文件并将其保留在同一位置C# 如何使用xamarin媒体插件让用户从图库中选择图像并重命名图像,c#,xamarin.forms,C#,Xamarin.forms,我正在使用xamarin媒体插件;允许用户选择图像并重命名图像 用户可以在使用相机拍照时重命名照片。但当用户使用xamarin媒体插件从gallery中选择时,如何重命名图像 我有下面的代码允许用户从图库中选择图像。当用户选择图像时;我将它保存在'file'变量中 我想重命名该文件并将其保留在同一位置 private async void Select_From_Gallery_Button_Clicked(object sender, EventArgs e) {
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");
我可以看到文件的完整路径。我想将名称从路径中删除,并将图像重命名为不同的名称您最好的选择就是使用不同的名称复制文件 首先创建接口(在Xamarin.Forms项目中) 然后,在每个平台上实现服务 安卓: 网间网操作系统 然后,在代码调用中:
var file = await CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
{
});
DependencyService.Get<IFile>().Copy(file.Path, "YourName.png");
var file=wait CrossMedia.Current.PickPhotoAsync(新Plugin.Media.Abstractions.PickMediaOptions
{
});
DependencyService.Get().Copy(file.Path,“YourName.png”);
[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);
}
}
}
[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");