Xamarin 使用按钮使用 FilePicker 选择文件
Xamarin use the file choose with FilePicker using a button
首先抱歉我的英语不好。我遇到的问题是当我创建一个按钮来选择 Android 文件夹中的文件时,但是当我写:
message.Attachments.Add("here I don't know");
我想选择用文件按钮选择的文件。
我被屏蔽了,我已经在网上搜索了很多,没有找到任何解决方案。
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AppETF.View.Menu.DetailViews.SignalementEM">
<ContentPage.Content>
<Grid VerticalOptions="FillAndExpand">
<Button Text="Piece jointe" x:Name="fileLabel" Clicked="File" BackgroundColor="White" FontSize="25"/>
<Button Text="Envoyer l'email" Clicked="SendSMTPMail" BackgroundColor="White" FontSize="25"/>
</Grid>
</ContentPage.Content>
Cs :
private async void File(object sender, EventArgs e)
{
var fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return;
byte[] data = fileData.DataArray;
fileLabel.Text = fileData.FileName;
}
public async Task SendSMTPMail(string numero, string heure, string retard, string motif)
{
var message = new MailMessage();
var smtpServer = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
message.From = (new MailAddress("***"));
message.To.Add("***");
message.Subject = ***;
message.Body = ***;
message.Attachments.Add("second probleme here");
smtpServer.Credentials = new NetworkCredential("***", "****");
smtpServer.UseDefaultCredentials = false;
smtpServer.EnableSsl = true;
smtpServer.Send(message);
await DisplayAlert("", "Email Envoyé", "Ok");
}
//Use this code For file picking
//On Buton method:
private void Button1_Click(object sender, System.EventArgs e)
{
Intent = new Intent();
Intent.SetType("image/*");
Intent.SetType("pdf/*");
Intent.SetType("doc/*");
Intent.SetAction(Intent.ActionGetContent);
//Intent.PutExtra(Intent.ExtraAllowMultiple, true);
StartActivityForResult(Intent.CreateChooser(Intent, "Select a File"), PickImageId);
//CreateChooser(Intent, "Select Picture"), PickImageId);
}
//On OnActivityResult Method:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
//Bitmap bitmap = (Bitmap)data.Extras.Get("Select Picture");
Android.Net.Uri uri = data.Data;
image.SetImageURI(uri);
//image.SetImageBitmap(bitmap);
//GetImageFromURI(uri);
Toast.MakeText(this, "Path is " + GetImageFromURI(uri), ToastLength.Long).Show();
}
}
//GetImageFromURI Method:
public string GetImageFromURI(Uri contentURI)
{
Android.Database.ICursor cursor = ContentResolver.Query(contentURI, null, null, null, null);
if (cursor != null)
{
cursor.MoveToFirst();
string documentId = cursor.GetString(0);
documentId = documentId.Split(':')[1];
cursor.Close();
cursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri,
null,
Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new[] { documentId },
null);
cursor.MoveToFirst();
string path = cursor.GetString(cursor.GetColumnIndex(Android.Provider.MediaStore.Images.ImageColumns.Data));
cursor.Close();
return path;
}
return contentURI.Path;
}
首先抱歉我的英语不好。我遇到的问题是当我创建一个按钮来选择 Android 文件夹中的文件时,但是当我写:
message.Attachments.Add("here I don't know");
我想选择用文件按钮选择的文件。
我被屏蔽了,我已经在网上搜索了很多,没有找到任何解决方案。
XAML:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="AppETF.View.Menu.DetailViews.SignalementEM">
<ContentPage.Content>
<Grid VerticalOptions="FillAndExpand">
<Button Text="Piece jointe" x:Name="fileLabel" Clicked="File" BackgroundColor="White" FontSize="25"/>
<Button Text="Envoyer l'email" Clicked="SendSMTPMail" BackgroundColor="White" FontSize="25"/>
</Grid>
</ContentPage.Content>
Cs :
private async void File(object sender, EventArgs e)
{
var fileData = await CrossFilePicker.Current.PickFile();
if (fileData == null)
return;
byte[] data = fileData.DataArray;
fileLabel.Text = fileData.FileName;
}
public async Task SendSMTPMail(string numero, string heure, string retard, string motif)
{
var message = new MailMessage();
var smtpServer = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
message.From = (new MailAddress("***"));
message.To.Add("***");
message.Subject = ***;
message.Body = ***;
message.Attachments.Add("second probleme here");
smtpServer.Credentials = new NetworkCredential("***", "****");
smtpServer.UseDefaultCredentials = false;
smtpServer.EnableSsl = true;
smtpServer.Send(message);
await DisplayAlert("", "Email Envoyé", "Ok");
}
//Use this code For file picking
//On Buton method:
private void Button1_Click(object sender, System.EventArgs e)
{
Intent = new Intent();
Intent.SetType("image/*");
Intent.SetType("pdf/*");
Intent.SetType("doc/*");
Intent.SetAction(Intent.ActionGetContent);
//Intent.PutExtra(Intent.ExtraAllowMultiple, true);
StartActivityForResult(Intent.CreateChooser(Intent, "Select a File"), PickImageId);
//CreateChooser(Intent, "Select Picture"), PickImageId);
}
//On OnActivityResult Method:
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
//Bitmap bitmap = (Bitmap)data.Extras.Get("Select Picture");
Android.Net.Uri uri = data.Data;
image.SetImageURI(uri);
//image.SetImageBitmap(bitmap);
//GetImageFromURI(uri);
Toast.MakeText(this, "Path is " + GetImageFromURI(uri), ToastLength.Long).Show();
}
}
//GetImageFromURI Method:
public string GetImageFromURI(Uri contentURI)
{
Android.Database.ICursor cursor = ContentResolver.Query(contentURI, null, null, null, null);
if (cursor != null)
{
cursor.MoveToFirst();
string documentId = cursor.GetString(0);
documentId = documentId.Split(':')[1];
cursor.Close();
cursor = ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri,
null,
Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new[] { documentId },
null);
cursor.MoveToFirst();
string path = cursor.GetString(cursor.GetColumnIndex(Android.Provider.MediaStore.Images.ImageColumns.Data));
cursor.Close();
return path;
}
return contentURI.Path;
}