Xamarin.Android Android 11 acceso denegado pdf

paul moreno 1 Reputation point
2021-11-12T20:07:26.227+00:00

Hola estoy teniendo este problema en la api 30 del android(Android 11), ya no tengo permisos para guardar y abrir el archivo en este caso es un pdf, que se necesita cambiar para solucionarlo estoy investigando pero hasta ahora no he encontrado una solución, gracias por su ayuda saludos y buen dia.

private async void MostrarPdf(Uri url,string Nombre)
{
var javafile1 = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
try
{
var HttpClient = new HttpClient();

            await HttpClient.GetByteArrayAsync(url).ContinueWith(data =>
            {
                var javafile = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads), "ReportesPdf");
                if (!javafile.Exists())
                {
                    javafile.Mkdirs();
                }
                if (Build.VERSION.SdkInt <= (BuildVersionCodes)29)
                {
                    string localfilename = "Reporte:" + DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Local).ToLocalTime().ToLongTimeString() + Nombre + ".pdf";
                    string localpath = System.IO.Path.Combine(javafile.AbsolutePath, localfilename);
                    if (data.Result != null)
                    {
                        File.WriteAllBytes(localpath, data.Result);
                        Intent intent = new Intent(Intent.ActionView);
                        Java.IO.File file = new Java.IO.File(localpath);
                        file.SetReadable(true);
                        var uri = Android.Net.Uri.FromFile(file);
                        intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                        intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
                        intent.SetDataAndType(uri, "application/pdf");
                        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                        if (pro != null) { pro.Dismiss(); }
                        try
                        {
                            StartActivity(intent);
                        }
                        catch (ActivityNotFoundException er)
                        {
                            if (pro != null) { pro.Dismiss(); }
                            Toast.MakeText(Application.Context, "Install a pdf viewer.", ToastLength.Long).Show();
                        }
                    }
                }
                else
                {
                    string localfilename = "Reporte_" + Nombre + ".pdf";
                    string localpath = System.IO.Path.Combine(javafile.AbsolutePath, localfilename);

                    if (data.Result != null)
                    {

                        File.WriteAllBytes(localpath, data.Result);
                        Intent intent = new Intent(Intent.ActionView);
                        Java.IO.File file = new Java.IO.File(localpath);
                        intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                        intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
                        intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);
                        intent.SetDataAndType(Android.Net.Uri.FromFile(file), "application/pdf");
                        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);
                        StartActivity(intent);
                        if (pro != null) { pro.Dismiss(); }
                        try
                        {
                            StartActivity(intent);
                        }
                        catch (ActivityNotFoundException er)
                        {
                            if (pro != null) { pro.Dismiss(); }
                            Toast.MakeText(Application.Context, "Install a pdf viewer.", ToastLength.Long).Show();
                        }
                    }

                }



            });
        }
        catch (Exception ex)
        {
            if (pro != null)
            {
                pro.Dismiss();
            }
            Toast.MakeText(Application.Context, "No se generaron los reportes, Por favor verifique su conexion a internet." + ex.Message, ToastLength.Long).Show();
        }
    }
Developer technologies | .NET | Xamarin
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.