Opening pdf on android device error: file exposed beyond app through intent.getdata()

In my application I download the pdf from an API and store the file on the local file system. Then I try to open the pdf file with the built in viewer of the user. The next message appear:
*.pdf file exposed beyond app through intent.getdata().

 public static void OpenPDF(Context context, string filePath)
        {            
            //Open it up
            Android.Net.Uri pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(filePath));
            Intent intent = new Intent(Intent.ActionView);
            intent.SetDataAndType(pdfPath, "application/pdf");
            intent.SetFlags(ActivityFlags.GrantReadUriPermission);
            intent.SetFlags(ActivityFlags.NewTask);
            intent.SetFlags(ActivityFlags.ClearWhenTaskReset);
            context.StartActivity(intent);
        }

Am i missing something?

Tagged:

Answers

  • YoussefAzaroualiYoussefAzarouali USMember ✭✭

    Thanks JoeManke, I will go and try that.

    Gr. Youssef

  • YoussefAzaroualiYoussefAzarouali USMember ✭✭

    @>; @JoeManke said:

    I tried do that but the application gives me an unhandled exception:

    Java.Lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.IllegalArgumentException: Missing

    This is my code:

    In manifest I dit this:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.FMSXMobileApp" android:installLocation="auto">
      <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
      <application android:label="FMSXMobileApp.Android">
    
        <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.mydomain.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">      
        </provider>
      </application>
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    </manifest>
    

    My code:

     public static void OpenPDF(Context context, byte[] bytes)
            {
                var folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                var filePath = CommonLibrary.Helpers.FileHelper.WriteFileFromByteArray(bytes, folder, "document.pdf");
    
                //Open it up
                Android.Net.Uri pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(filePath));
                Intent intent = new Intent(Intent.ActionView);
                intent.SetDataAndType(pdfPath, "application/pdf");
                intent.SetFlags(ActivityFlags.GrantReadUriPermission);            
                intent.SetFlags(ActivityFlags.NewTask);            
                intent.SetFlags(ActivityFlags.ClearWhenTaskReset);
                context.StartActivity(intent);
            }
    

    Do you have any idea what I am missing?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    You need to use the FileProvider to get the URI.

    // Android.Net.Uri pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(filePath));
    Android.Net.Uri pdfPath = FileProvider.GetUriForFile(context, "com.mydomain.fileprovider", new Java.IO.File(filePath));
    
  • YoussefAzaroualiYoussefAzarouali USMember ✭✭

    @JoeManke said:
    You need to use the FileProvider to get the URI.

    // Android.Net.Uri pdfPath = Android.Net.Uri.FromFile(new Java.IO.File(filePath));
    Android.Net.Uri pdfPath = FileProvider.GetUriForFile(context, "com.mydomain.fileprovider", new Java.IO.File(filePath));
    

    Thank you for your info. The error is now gone, but when I try to do this:

    Android.Net.Uri pdfPath = FileProvider.GetUriForFile(context, "com.mydomain.fileprovider", new Java.IO.File(filePath));

    I get an error:
    Java.Lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
    at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in :0
    at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters) [0x00018] in :0
    at Android.Support.V4.Content.FileProvider.GetUriForFile (Android.Content.Context context, System.String authority, Java.IO.File file) [0x00070] in :0
    at FMSXMobileApp.Droid.DocumentView.FMSXMobileApp.Interfaces.IDocumentView.DocumentView (System.Byte[] bytes, System.String name, System.String title) [0x000aa] in D:\FMSXAPP\FMSXMobileApp\FMSXMobileApp.Android\DocumentView.cs:38
    --- End of managed Java.Lang.NullPointerException stack trace ---
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:591)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:565)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:403)
    at mono.java.lang.RunnableImplementor.n_run(Native Method)
    at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6923)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
    }

    Any ideas on how to solve this issue?

Sign In or Register to comment.