[英]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.