I am developing an App on Xamarin.Android and I get the following error when I try to create a PDF on the device ...
System.UnauthorizedAccessException: Access to the path "/ mnt / sdcard-ext" is denied.
The weird thing is that in the android manifest I have checked the read and write permissions.
This is the code I use to create the PDF
//Uso el directorio "/mnt/sdcard-ext" que es la ruta original de la memoria externa, de lo contrario crea un directorio emulado y no puedo ver el pdf en el dispositivo si lo busco manualmente
var directory = new Java.IO.File("/mnt/sdcard-ext", "pdf").ToString();
if (!Directory.Exists(directory)){
Directory.CreateDirectory(directory); //Aquí me da el error
}
var path = System.IO.Path.Combine(directory, "PdfTest.pdf");
if (File.Exists(path)){
File.Delete(path);
}
var fs = new FileStream(path, FileMode.Create);
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
document.Add(new Paragraph("Hola mundo!"));
document.Close();
writer.Close();
fs.Close();
Java.IO.File file = new Java.IO.File(path);
Intent intent = new Intent(Intent.ActionView);
intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
StartActivity(intent);