saving file to SD-card for public access

AndreyZhukovAndreyZhukov ✭✭USMember ✭✭

How in Xamarin.Android save the file to an external source (SD card)?
There is a task to save the file so that the user can then work with this file for review and outside the application.
The code below is working, to save the file in a specialized directory. But this directory is hidden from the user and you can not work with these files.

public class FileWorker_Android : IFileWorker
    {
        public Task DeleteAsync(string filename)
        {
            File.Delete(GetFilePath(filename));
            return Task.FromResult(true);
        }

        public Task<bool> ExistsAsync(string filename)
        {
            string filepath = GetFilePath(filename);
            // существует ли файл
            bool exists = File.Exists(filepath);
            return Task<bool>.FromResult(exists);
        }

        public Task<IEnumerable<string>> GetFilesAsync()
        {
            IEnumerable<string> filenames = from filepath in Directory.EnumerateFiles(GetDocsPath())
                                            select Path.GetFileName(filepath);
            return Task<IEnumerable<string>>.FromResult(filenames);
        }

        public async Task<string> LoadTextAsync(string filename)
        {
            string filepath = GetFilePath(filename);
            using (StreamReader reader = File.OpenText(filepath))
            {
                return await reader.ReadToEndAsync();
            }
        }

        public async Task SaveTextAsync(string filename, string text)
        {
            string filepath = GetFilePath(filename);
            using (StreamWriter writer = File.CreateText(filepath))
            {
                await writer.WriteAsync(text);
            }
        }
        string GetFilePath(string filename)
        {
            return Path.Combine(GetDocsPath(), filename);
        }
        // Directory MyDocuments
        string GetDocsPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }
    }

From the Android documentation, all work with an external source (SD card) occurs via Environment.getExternalStoragePublicDirectory, but in Xamarin in Environment there is no such method

Best Answer

Answers

  • DimChrisDimChris ✭✭✭ USMember ✭✭✭

    First question what kind of file needs user to save? Example a txt file? I have various codes which i can use to help you. I think you doesnt need to use so big code for that.

  • AndreyZhukovAndreyZhukov ✭✭ USMember ✭✭

    @DimChris said:
    First question what kind of file needs user to save? Example a txt file? I have various codes which i can use to help you. I think you doesnt need to use so big code for that.

    Yes, e.g. txt file

  • AndreyZhukovAndreyZhukov ✭✭ USMember ✭✭

    @DimChris said:
    First question what kind of file needs user to save? Example a txt file? I have various codes which i can use to help you. I think you doesnt need to use so big code for that.

    Can you help me?

  • DimChrisDimChris ✭✭✭ USMember ✭✭✭

    http://www.c-sharpcorner.com/UploadFile/f0b2ed/write-and-read-data-from-sd-card/
    If you find difficult to try this, tell me to send you just an easy example.

  • AndreyZhukovAndreyZhukov ✭✭ USMember ✭✭
    edited March 2018

    @DimChris said:
    ...
    If you find difficult to try this, tell me to send you just an easy example.

    Environment.getExternalStoragePublicDirectory, but in Xamarin in Environment there is no such method

  • DimChrisDimChris ✭✭✭ USMember ✭✭✭
    edited March 2018
    var sdcardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
                    var FilePath = System.IO.Path.Combine(sdcardPath, "text.txt");//Φάκελος και εικόνα
                    if(!System.IO.File.Exists(FilePath))
                    {
                        using (System.IO.StreamWriter writer = new System.IO.StreamWriter(FilePath, true))
                        {
                            writer.WriteLine("blablabla");
                        }
                    }
    

    Xamarin is simulately with java, but unfortunately there is not big community and much examples for searching

  • AndreyZhukovAndreyZhukov ✭✭ USMember ✭✭

    @DimChris said:
    var sdcardPath = Android.OS.Environment.ExternalStorageDirectory.Path;

    "Android" does not exist in the current context.
    I can use Xamarin.Forms.PlatformConfiguration.Android.OS.Environment.ExternalStorageDirectory.Path; but "Android" does not contain a definition "OS"


    I can not attach a screenshot because of a new account

  • ZeneriZeneri ✭✭ Member ✭✭
    edited June 17

    I found this solution and tried it on a Zebra MC33 device. In File Browser I can see the path /sdcard/MyFolder which I created trough an USB connection using Windows Explorer. In this folder I placed MyApp.ini. When I try to read from

    var sdcardPath = Android.OS.Environment.ExternalStorageDirectory.Path + System.IO.Path.DirectorySeparatorChar + "MyFolder" + System.IO.Path.DirectorySeparatorChar + "MyApp.ini";

    I get the message:

    Access to the path "/storage/emulated/0/MyFolder/MyApp.ini" is denied.

    What did I wrong?

Sign In or Register to comment.