0

I'm able to save a pdf file locally on the phone in the downloads folder. Now I want to be able to open the file in the generic pdf viewer. This is the file I have for example:

enter image description here

When opening directly from the downloads folder I'm getting this view:

enter image description here

How can I open the document in the same viewer as soon as the document is downloaded?

I tried this:

    Device.OpenUri(new System.Uri("file:///storage/emulated/0/Download/740067_Invoice_Food.pdf"));

But nothing is happening.

halfer
  • 19,824
  • 17
  • 99
  • 186
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • Try this reference https://blog.verslu.is/xamarin/xamarin-forms-xamarin/showing-pdf-files-xamarin-forms/ – Himanshu Dwivedi Oct 16 '19 at 16:28
  • Thanks but from what I can see this still uses as view, its not opening up in the native pdf viewer – Laziale Oct 16 '19 at 16:33
  • @Laziale, Do you try to use dependencyService to open PDF, take a look:https://stackoverflow.com/questions/27820246/how-to-view-pdf-file-using-xamarin-forms – Cherry Bu - MSFT Oct 17 '19 at 07:08

2 Answers2

1

Try this approach, maybe you'll need to change folder path using Environment.SpecialFolder

var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var filename = Path.Combine(documents, "MyPDF.pdf");
            Device.OpenUri(new Uri(filename));
Guilherme Nimer
  • 555
  • 2
  • 14
  • Thanks, doesn't really work since the documents path points to the app folder not to the downloads – Laziale Oct 16 '19 at 16:34
1

First specify fileprovider into android manifest file

Something like this

 <application android:label="The name of your project" android:supportsRtl="true" android:icon="@drawable/register">
  <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"></meta-data>
  </provider>
 </application>

Next, add provider_paths.xml file into xml folder

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

Now you should call native method for each platform by 'DependencyService' to open pdf file

Do it according to this GREAT Doc

https://officialdoniald.azurewebsites.net/2019/09/24/xamarin-forms-save-and-open-pdf-file/

user193679
  • 181
  • 2
  • 6