2

I am trying to download and open a PDF file from a URL in Xamarin Android. I can open the URL no problem, which of course saves the PDF in the Downloads folder. Is there a way I can then access that same file and have it open automatically after being downloaded within my app? Here is how I am currently opening the URL to download:

Intent i = new Intent(Intent.ActionView); Intent browserIntent = new Intent(Intent.ActionView, Uri.Parse("http://sampleURL.com")); StartActivity(browserIntent); 

1 Answer 1

3

You would first have to know the name of the file that was download.

Then you could do this

var filename = "file.pdf"; var file = System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).ToString(), filename); var uri = Android.Net.Uri.Parse(file); Intent intent = new Intent(Intent.ActionView); intent.SetFlags(ActivityFlags.ClearTop); intent.SetDataAndType(uri, "application/pdf"); try { StartActivity(intent); } catch (ActivityNotFoundException e) { Toast.MakeText(Application.Context, "Install a pdf viewer.", ToastLength.Long).Show(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for answering! I wound up just opening the PDF with the default PDF viewer app since I was accessing it with a URL, but this will come in handy when we go a different route.
From Android 7, this solution will not work you have to use FileProvider
@Abdurakhmon can you provide a full working example, please?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.