Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to pick some file from file picker in xamarin android?

BayuBayu Member ✭✭✭
I want to attach some file in my app function.

I take a file by using file picker, but in my code I only can add one file.

How to pick some file in xamarin android? Thanks

Answers

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    Try to set the extra data to ExtraAllowMultiple which allows the user to select and return multiple items.

    Intent intent = new Intent();
    intent.SetType("image/*");
    intent.PutExtra(Intent.ExtraAllowMultiple, true);
    intent.SetAction(Intent.ActionGetContent);
    StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 1);
    

    Refer to:
    https://stackoverflow.com/questions/19513556/select-multiple-files-with-intent-action-get-content

  • BayuBayu Member ✭✭✭

    thank you @Jarvan, iam sorry, I use the smtp method to send an email. I have two button, button 2 is send button, button 1 Is attach button, this is my code

          btn1.Click += Btn1_Click;
    
                            btn2.Click += (s, e) =>
                            {
                                if (a == "" || a == null)
                                {
                                    AlertDialog.Builder alertdialog1 = new AlertDialog.Builder(this);
                                    alertdialog1.SetTitle("Alert");
                                    alertdialog1.SetMessage("Please attach file!");
                                    alertdialog1.SetNeutralButton("Ok", delegate { alertdialog1.Dispose(); });
                                    alertdialog1.Show();
                                }
                                else
                                {
                                    try
                                    {
                                        MailMessage mail = new MailMessage();
    
                                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    
    
    
                                        mail.From = new MailAddress("[email protected]");
    
                                        mail.To.Add(edt1.Text);
    
                                        mail.Subject = edt2.Text + "/" + edt4.Text ;
    
                                        mail.Body = edt3.text
                                        mail.Attachments.Add(new Attachment(edt5.Text));
    
    
                                        SmtpServer.Port = 587;
    
                                        SmtpServer.Host = "smtp.gmail.com";
    
                                        SmtpServer.EnableSsl = true;
    
                                        SmtpServer.UseDefaultCredentials = false;
    
                                        SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "**********");
    
                                        ServicePointManager.ServerCertificateValidationCallback = delegate (object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
                                        {
                                            return true;
                                        };
    
                                        SmtpServer.Send(mail);
                                        AlertDialog.Builder alertdialog2 = new AlertDialog.Builder(this);
                                        alertdialog2.SetTitle("Alert");
                                        alertdialog2.SetMessage("Email sent!");
                                        alertdialog2.SetNeutralButton("Ok", delegate { alertdialog2.Dispose(); });
                                        alertdialog2.Show();
                                    }
    
                                    catch (Exception ex)
    
                                    {
    
                                        AlertDialog.Builder alertdialog3 = new AlertDialog.Builder(this);
                                        alertdialog3.SetTitle("Alert");
                                        alertdialog3.SetMessage("Sending Email Failure!");
                                        alertdialog3.SetNeutralButton("Ok", delegate { alertdialog3.Dispose(); });
                                        alertdialog3.Show();
    
                                    }
                                }
    
    
    
                            };
                        }
    
                        [Obsolete]
                        private async void Btn1_Click(object sender, EventArgs e)
                        {
                            try
                            {
                                FileData fileData = new FileData();
                                fileData = await CrossFilePicker.Current.PickFile();
                                byte[] data = fileData.DataArray;
                                string name = fileData.FileName;                
                                string filePath = fileData.FilePath;
                                a = filePath.ToString();
                                edt5.Text = filePath.ToString();
                            }
                            catch (Exception ex)
                            {
    
                            }
                        }
    

    but in this code I only can send one file attachment, how to make multiple attachment?

  • YelinzhYelinzh Member, Xamarin Team Xamurai

    You could get the file path from the intent in OnActivityResult method.

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == 1)
        {
            if (data != null)
            {
                var uri = data.Data;
                string path = GetActualPathFromFile(uri);
            }
        }
    }
    
    private string GetActualPathFromFile(Android.Net.Uri uri)
    {
        //...
        return null;
    }
    

    Similar issue:
    https://stackoverflow.com/questions/46975574/how-to-get-actual-path-from-uri-xamarin-android/47008332#47008332

Sign In or Register to comment.