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 attach internal storage file in xamarin android?

BayuBayu Member ✭✭✭

I made an email sending form using the smtp method. I added one attachment button, but my attachment button only succeeded in attaching the file in external storage.

How can I attach files that are in internal storage? Thank you.

Best Answers

  • BayuBayu Member ✭✭✭
    Accepted Answer

    thank you @jezh, I think I already know the problem why I can't attach the file.
    I archive the app in the release of any CPU and this makes my app unable to attach files.

    At the time of making the demo I tried to archive the app in debug any CPU and the app work properly.

    Thanks for the help, great discussion.

    Nice discuss with you, have a nice day @jezh. :)

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can check official document File Storage and Access with Xamarin.Android

    Then we will find relative knowledge about Internal Storage and External Storage

    Internal Storage – this is a portion of the file system that can be accessed only by the application or the operating system.
    External Storage – this is a partition for the storage of files that is accessible by all apps, the user, and possibly other devices. On some devices, external storage may be removable (such as an SD card).

    When we check Internal vs external storage, we will find

    Internal storage refers to the non-volatile memory that Android allocates to the operating system, APKs, and for individual apps. This space is not accessible except by the operating system or apps. Android will allocate a directory in the internal storage partition for each app. When the app is uninstalled, all the files that are kept on internal storage in that directory will also be deleted. Internal storage is best suited for files that are only accessible to the app and that will not be shared with other apps or will have very little value once the app is uninstalled.

    So, we couldn't access directly the internal storage, besides the app itself ( internal storage for individual apps) or operating system.

  • BayuBayu Member ✭✭✭
    edited April 2020

    Thank you @jezh, after reading this, I think I need access to external storage.

    I make two button in my app, button1 is attach button, button 2 is sending 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)
                        {
    
                        }
                    }
    

    this code is working properly if I choose file in sdCard, but when I choose file in internal storage of phone, which the directory is storage/emulated/0/…..the file can't attach.

    any solution for this problem? thanks @jezh

  • BayuBayu Member ✭✭✭

    thank you @jezh, I have used the permission

        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    but the files from storage / emulated / 0 / ... cannot be attached, please help me!

  • jezhjezh Member, Xamarin Team Xamurai

    What exactly is your problem?Could you please post more details about this question?

    Besides, have you checked the sample ? This sample tell use how to access External storage.

  • BayuBayu Member ✭✭✭

    thank you @jezh,

    I'am sorry, I dont konw exactly what is my problem. My problem is:

    When I build the app on the Android Visual Studio emulator I can attach files. Which the directory is storage / sdcard / ....

    but when I archive, distribute and install the app in my phone, I can't attach the file.

    As additional information, I don't have an sdcard in my phone so my file directory is storage / emulated / 0 / ....

    I think this is due to my application not being able to read external storage but after I used the permission

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    the application still doesn't work properly. How to solve it?

  • jezhjezh Member, Xamarin Team Xamurai

    If it is convenient for you, could you please share a basic demo so that we can test on our side?

  • BayuBayu Member ✭✭✭

    Of course, thank you. @jezh

    please wait a moment!

  • BayuBayu Member ✭✭✭
    Accepted Answer

    thank you @jezh, I think I already know the problem why I can't attach the file.
    I archive the app in the release of any CPU and this makes my app unable to attach files.

    At the time of making the demo I tried to archive the app in debug any CPU and the app work properly.

    Thanks for the help, great discussion.

    Nice discuss with you, have a nice day @jezh. :)

  • jezhjezh Member, Xamarin Team Xamurai

    Wow, congrats. Happy coding... :)

Sign In or Register to comment.