Xamarin Forms: accessing external storage multi-platform VS-2019 (android app)

tim 80 Reputation points
2024-03-10T01:23:29.3466667+00:00

I'm trying to create an App for a android tablet to record collection data to a file then off load/copy the file to a PC to upload to a database.

I would like to try to write the data to a file on the removable sim card.

But when using the following in my xaml code behind, ( in the main csproj file)

string rootPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);

I'm getting the following error

Error CS0117 'Android' does not contain a definition for 'OS'

Am I missing a Using? there is no help from the "Quick Action"

(current using)

using System;

using System.IO;

using System.Text;

using Xamarin.Forms;

using Xamarin.Forms.PlatformConfiguration;

using Xamarin.Forms.Xaml;

also

Read write permission are checked in the Android manifest.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,595 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,121 Reputation points Microsoft Vendor
    2024-03-13T02:23:05.2266667+00:00

    Hello,

    I'm trying to create an App for a android tablet to record collection data to a file then off load/copy the file to a PC to upload to a database.

    According to your description, you want to copy files directly from Android to computer, which is not feasible. Because Android has a different file system than Windows. And Android has modified and restricted the permissions of external storage after 11, and you can no longer store files directly to external folders.

    You could refer to the answer from Android 11 external storage access for more details.

    To do this, you need to remediate the file to a relayed path that is accessible to both, and then get the file from Windows.

    Please refer to the following code and documentation.

    Step 1. Implement a way to save and share files.

    public class FileHelperService
    {
        public void SaveFile(string filename)
        {
            var path = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, filename);
    
    
            // save the file
            File.WriteAllText(path, "Hello World");
        }
    
    
        public async Task ShareFileAsync()
        {
            var fn = "test.db";
            var file = Path.Combine(FileSystem.AppDataDirectory, fn);
            await Share.RequestAsync(new ShareFileRequest
            {
                Title = "Share DB",
                File = new ShareFile(file)
            });
        }
    }
    
    // This method can be called in the event that you can click on a button
    private async void Button_Clicked(object sender, EventArgs e)
    {
        FileHelperService filehelper = new FileHelperService();
        await filehelper.ShareFileAsync();
    }
    

    Step 2. With the Share method, you can relay your database file by mail and download it in Windows.

    Please refer to Xamarin.Essentials: Share for more details.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. tim 80 Reputation points
    2024-03-13T20:57:04.9666667+00:00

    Thanks for explaining the process, I did not know about the restriction on android.

    I'll look at adding the share functionality to my app.

    I'll be closing this post.

    Again Thanks for all your help.

    0 comments No comments