0

Tengo un método que recibe un arreglo de Bytes, el cual contiene un pdf, mi problema es que no se como abrir el pdf, o como indicarle que lo abra con una app ya instalada en el móvil, en la línea dónde está el siguiente código:

intent.SetDataAndType(global::Android.Net.Uri.FromFile(localImage), "application/pdf");

Cómo le indico que aplicación abrirá el pdf? quiero que me muestre las app's que sean capaces de abrir el pdf

public void OpenPdf()
            {
                try
                {
                    byte[] imageBytes = pdf;
                    string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                    string localFilename = "pdfRecibo.pdf";
                    string localPath = System.IO.Path.Combine(documentsPath, localFilename);
                    File.WriteAllBytes(localPath, imageBytes); // writes to local storage   
                    var localImage = new Java.IO.File(localPath);
                    if (localImage.Exists())
                    {
                        global::Android.Net.Uri uri = global::Android.Net.Uri.FromFile(localImage);
                        var intent = new Intent(Intent.ActionView, uri);
                        //intent.SetType ("application/pdf");
                        intent.SetDataAndType(global::Android.Net.Uri.FromFile(localImage), "application/pdf");
                        this.StartActivity(intent);
                    }
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, "error: " + ex.Message, ToastLength.Short).Show();
                }
            }

1 respuesta 1

1

Ya pude abrir el pdf, sólo mando cómo parámetro el arreglo de bytes[] y el nombre del archivo, sobre cómo lo llamaré

        public async Task<string> 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);
            return filePath;
        }

        public void OpenFile(string filePath, string filename)
        {

            var bytes = File.ReadAllBytes(filePath);

            //Copy the private file's data to the EXTERNAL PUBLIC location
            string externalStorageState = global::Android.OS.Environment.ExternalStorageState;
            string application = "";

            string extension = System.IO.Path.GetExtension(filePath);

            switch (extension.ToLower())
            {
                case ".doc":
                case ".docx":
                    application = "application/msword";
                    break;
                case ".pdf":
                    application = "application/pdf";
                    break;
                case ".xls":
                case ".xlsx":
                    application = "application/vnd.ms-excel";
                    break;
                case ".jpg":
                case ".jpeg":
                case ".png":
                    application = "image/jpeg";
                    break;
                default:
                    application = "*/*";
                    break;
            }
            var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/" + filename + extension;
            File.WriteAllBytes(externalPath, bytes);

            Java.IO.File file = new Java.IO.File(externalPath);
            file.SetReadable(true);
            //Android.Net.Uri uri = Android.Net.Uri.Parse("file://" + filePath);
            Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(uri, application);
            intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

            try
            {
                this.StartActivity(intent);
            }
            catch (Exception)
            {
                Toast.MakeText(this, "No hay aplicaciones instaladas para abrir PDF's", ToastLength.Short).Show();
            }
        }
1
  • oye el metodo StartActivity me dice que no existe porque se debe eso?
    – Marcos
    el 3 oct. 2018 a las 17:47

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.