I have problem in giving permission to a route to export there a pdf in xamarin android how can I do it: here I have what I have done
//Define permisos requeridos por aplicación.
readonly string[] PermissionsMyApp =
{
Manifest.Permission.WriteExternalStorage,
Manifest.Permission.ReadExternalStorage
};
private void permiso()
{
const int RequestExternalStorageId = 0;
const string permission = Manifest.Permission.WriteExternalStorage;
if (CheckSelfPermission(permission) == (int)Permission.Granted)
{
//Ya se cuenta con permisos!
exportarPDF();
return;
}
//Se necesita obtener permiso,
if (ShouldShowRequestPermissionRationale(permission))
{
//Explica al usuario porque necesita el permiso.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetMessage("Permiso requerido para el correcto funcionamiento de la aplicación.").SetTitle("Permiso").SetPositiveButton("OK", delegate { RequestPermissions(PermissionsMyApp, RequestExternalStorageId); }).SetNegativeButton("No", delegate { }).Show();
return;
}
//Finalmente requiere el permiso permissions and Id
RequestPermissions(PermissionsMyApp, RequestExternalStorageId);
}
private void exportPDF () {
/* PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(597, 820, 1).Create();
PdfDocument.Page page = document.StartPage(pageInfo);*/
// create a new document
PdfDocument document = new PdfDocument();
/* EditText content2 = new EditText(context);
content2.Text = "Cat";*/
// crate a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(200, 100, 1).Create();
// start a page
PdfDocument.Page page = document.StartPage(pageInfo);
// draw something on the page
content2.Draw(page.Canvas);
// finish the page
document.FinishPage(page);
//create the outputstream to sdcard
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filePath = System.IO.Path.Combine(sdCardPath, "Médico de familia.pdf");
// Directory.CreateDirectory(filePath);
var stream = new FileStream(filePath, FileMode.Create);// me da error de autorizacion pqqqqqq
// write the document content
document.WriteTo(stream);
// close the document
document.Close();
Toast.MakeText(ApplicationContext, "PDF Generated", ToastLength.Short).Show();
}