[英]How I can enable user to select image from gallery using xamarin media plugin and rename the image
我正在使用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");
我可以看到文件的完整路径。 我想从路径中获取名称并将图像重命名为其他名称
最好的选择就是简单地使用其他名称复制文件。
首先创建您的界面(在您的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);
}
}
}
[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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.