I can not get the actual path of the SD on my android device in Xamarin.forms

3

I am developing an App in Xamarin.forms in which I must generate a PDF inside the SD card of the device.

The problem I have is that every time I try to access the SD it always returns the same result:

  

/storage/emulated/0/HolaMundo.pdf

This is the sentence I use to get the directory ... Android.OS.Environment.ExternalStorageDirectory.Path; Although it's a sentence that is used only for android, what I did was create a class in the PCL project which will save the directory that I get from the MainActivity.cs of the Android project.

As I mentioned before, the directory that returns is an emulated external memory, which would not be the SD that I have physically in the device. Therefore, every time I create a file, I can not find it on the device.

I leave here the project to download and analyze it in case you have not explained it well

Here is the code of when I get the route of the SD

 Configuraciones.ExternalPathApp = Android.OS.Environment.ExternalStorageDirectory.Path; //Esto devuelve "/storage/emulated/0"
 Configuraciones.PathApp = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); //Esto devuelve "/data/user/0/GenerarPDF.Android/files"

As far as I understand, these directories can only be accessed in case the device is rooted. In my case this App will not be used with rooted devices

Here is the code when I generate the PDF

private void BtnPDF_Clicked(object sender, EventArgs e)
{
    string filename = Configuraciones.PathApp + "/HolaMundo.pdf";
    string externo = Configuraciones.ExternalPathApp + "/HolaMundo.pdf";

    using (var stream = GenerateStreamFromString(filename))
    {
        Document doc = new Document(PageSize.A4);
        PdfWriter escritor = PdfWriter.GetInstance(doc, stream);
        doc.AddTitle("HolaMundo");
        doc.AddCreator("Matia Molina");
        doc.Open();
        PdfContentByte contentByte = escritor.DirectContent;
        doc.Add(new Paragraph("Hola Mundo"));
        doc.Close();
        escritor.Close();
    }

    using (var stream = GenerateStreamFromString(externo))
    {
        Document doc = new Document(PageSize.A4);
        PdfWriter escritor = PdfWriter.GetInstance(doc, stream);
        doc.AddTitle("HolaMundo");
        doc.AddCreator("Matia Molina");
        doc.Open();
        PdfContentByte contentByte = escritor.DirectContent;
        doc.Add(new Paragraph("Hola Mundo"));
        doc.Close();
        escritor.Close();
    }

}

I also tried creating files .txt , but in no case can I check the files, neither the PDF nor the TXT.

    
asked by Matias 03.04.2018 в 17:02
source

2 answers

1

To access the device memory, we have the following directories: "/" is the root of the device, "/ mnt / sdcard" is the root of the internal memory and "/ mnt / sdcard-ext" is the root of the external memory (SD) ... This data had to put them on hand from the MainActivity.cs since otherwise I could not access these directories. This works correctly since from the MainActivity I was able to create a test folder.

    
answered by 05.04.2018 / 15:19
source
0

Even though I do not keep the files in the same place where you want to save them, I put you as I do to see if it helps you.

In the PCL project I have this interface

using System.Threading.Tasks;

namespace Proyecto.Interfaces
{
    public interface IFiles
    {
        void SaveFiles(string filename, byte[] bytes);
    }
}

And on Android I have this implementation

using Android.Content;
using Proyecto.Interfaces;
using Proyecto.Droid.Interfaces;
using System.IO;
using Xamarin.Forms;

[assembly: Dependency(typeof(FilesImplementation))]

namespace Proyecto.Droid.Interfaces
{
    class FilesImplementation : IFiles
    {
        public void SaveFiles(string filename, byte[] bytes)
        {
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            var filePath = Path.Combine(documentsPath, filename);
            File.WriteAllBytes(filePath, bytes);
            OpenFile(filePath, filename);
        }
     }
   } 

When I want to record a file I do it like this:

        var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
        if (status != PermissionStatus.Granted)
        {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
                if (results.ContainsKey(Permission.Storage))
                       status = results[Permission.Storage];
         }
         if (status == PermissionStatus.Granted)
         {
                DependencyService.Get<IFiles>().SaveFiles(item.Filename, item.FicheroRaw);
         }

I hope it serves you

    
answered by 04.04.2018 в 10:16