Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to open a PDF (Android) that you downloaded (SOLUTION)

$
0
0

I've seen a lot of questions on how in Android to view a PDF that was downloaded by the app.

Here's an easy solution I found that works for me. My app downloads report pdfs and need to show them.

Basically you copy the file from the private storage to the external storage. That's it.

        //Read all the PDF data from the LOCAL PRIVATE STORAGE where you saved it to
       //PathToFile is just a function that gets the path I wrote
        var bytes = File.ReadAllBytes(PathToFile(fileNameofPrivateFile));

        //Copy the private file's data to the EXTERNAL PUBLIC location
        var externalPath = global::Android.OS.Environment.ExternalStorageDirectory.Path + "/report.pdf";
        File.WriteAllBytes(externalPath, bytes);

        //Open it up
        Uri pdfPath = Uri.Parse(externalPath);
        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(pdfPath, "application/pdf");
        intent.SetFlags(ActivityFlags.NewTask);
        Forms.Context.StartActivity(intent);

Viewing all articles
Browse latest Browse all 89864

Trending Articles