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.