Forum Xamarin.Forms

How to open PDF or TXT file in default app on Xamarin Forms

I am going to open document using default app in Xamarin Forms. I tried already this approach but it doesn't work for me and I am not sure what is the reason.

Device.OpenUri(new Uri(FILE_PATH));

Please give me great solution if anyone knows how to handle it. Thanks.

Tagged:

Best Answer

Answers

  • kikolopes90kikolopes90 PTMember ✭✭
    edited September 2017
  • @kikolopes90 It would be help but not solution. Thanks.

  • GiampaoloGabbaGiampaoloGabba USMember ✭✭✭
  • afranz33afranz33 Member

    @PassionateCreative Where do you place this code? I'm new to the Xamarin world trying to figure this all out. I have a .net standard shared and the 2 projs fordroid and iOS.

  • RussHardieRussHardie USMember ✭✭

    The CrossFilePicker component has a OpenFile method
    https://github.com/Studyxnet/FilePicker-Plugin-for-Xamarin-and-Windows/

  • It's not working, unfortunately using OpenFile from FilePicker. Any ideas on how to open files using Xamarin Forms?

  • TiagoDiasESTTiagoDiasEST Member ✭✭

    Any ideas of how to open files in android 7 or higher without using the FileProvider? I'm looking for other way to do this and nothing coming up? Any miracle ideas for this?

  • AlexMAlexM Member ✭✭
    edited January 2019

    @PassionateCreative 's implementation does work. Use his implementation on iOS. Thanks

  • Ganesh.4229Ganesh.4229 USMember ✭✭

    @PassionateCreative I am receiving the below warning from iOS

    Warning: Attempt to present on whose view is not in the window hierarchy!

    I have implemented a dependency service and tried to open the file.

    Below is my code for iOS.
    public void OpenFile(string filepath)
    {
    var PreviewController = UIDocumentInteractionController.FromUrl(NSUrl.FromFilename(filepath));
    PreviewController.Delegate = new UIDocumentInteractionControllerDelegateClass(UIApplication.SharedApplication.KeyWindow.RootViewController);
    Device.BeginInvokeOnMainThread(() =>
    {
    PreviewController.PresentPreview(true);
    });
    }

    public class UIDocumentInteractionControllerDelegateClass : UIDocumentInteractionControllerDelegate
    {
        UIViewController ownerVC;
    
        public UIDocumentInteractionControllerDelegateClass(UIViewController vc)
        {
            ownerVC = vc;
        }
    
        public override UIViewController ViewControllerForPreview(UIDocumentInteractionController controller)
        {
            return ownerVC;
        }
    
        public override UIView ViewForPreview(UIDocumentInteractionController controller)
        {
            return ownerVC.View;
        }
    }
    
  • AlexMAlexM Member ✭✭
    edited January 2019

    @PassionateCreative Code works but I had a problem on iOS. It started to froze the UI when it tried to PresentPreview.
    Same code was working on simulator but it didn't work on iPad.
    There was a bug on iOS iPad (my case) and it need to be restarted.
    More Info About freezing the UI:
    https://stackoverflow.com/questions/48263792/ios11-uidocumentinteractioncontroller-presentpreviewanimated-freezes-the-app

    I restarted the iPad and it works.
    Here is another useful link backing up @PassionateCreative implementation.
    Previewing Files Inside Your Xamarin.iOS App - by Jimmy Garrido July 25, 2018 iOS
    https://blog.xamarin.com/previewing-files-with-xamarin-ios/

    Also we can have a look here:
    https://forums.xamarin.com/discussion/13793/how-to-implement-uidocumentinteractioncontroller-on-ipad

  • ranju_ranju_ Member ✭✭
    edited March 2019

    In case anyone getting Android.OS.FileUriExposedException while using @PassionateCreative implementation for Android, simply put
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    in Application.onCreate() method

    Below link contains a good discussion on the same
    https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

  • NavidNavidNavidNavid USMember ✭✭

    @ranju_ said:
    In case anyone getting Android.OS.FileUriExposedException while using @PassionateCreative implementation for Android, simply put
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    in Application.onCreate() method

    Below link contains a good discussion on the same
    https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

    Thanks bro
    I need just this

    "StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    in Application.onCreate() method"

  • JimmyMcDermottJimmyMcDermott USMember ✭✭

    @ranju_ said:
    In case anyone getting Android.OS.FileUriExposedException while using @PassionateCreative implementation for Android, simply put
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    in Application.onCreate() method

    Below link contains a good discussion on the same
    https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

    I am using this and it appears to work, except that when I select an application open the PDF with I am getting a "Cannot open document. Document is corrupted error." I know I am saving the document correctly as this was working previously on Android 7.1

    Is there something else I need to add for this to work?

Sign In or Register to comment.