We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Android 9.0 (API 28) throws exception when viewing the pdf file

Hi,

After creating a pdf file using syncfusion I try to show the file to the user using the following code snippet:

  if (file.Exists())
            {
                Android.Net.Uri path = Android.Net.Uri.FromFile(file);
                string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString());
                string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
                Intent intent = new Intent(Intent.ActionView);
                //intent.SetFlags(ActivityFlags.NewTask);
                intent.SetDataAndType(path, mimeType);
                Android.App.Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));
            } 

I have tested this on so many devices (with different API levels) and it's been working fine until I tested it on Android 9.0 (API level 28), on line 
      "Android.App.Application.Context.StartActivity(Intent.CreateChooser(intent, "Choose App"));"

it throws this exception: Android.OS.FileUriExposedException: <Timeout exceeded getting exception details>

could you please help me to resolve the issue?

P.S: I have tested this on both device and emulator, had same exception on both of them

Best regards,
Reihaneh



5 Replies

SL Sowmiya Loganathan Syncfusion Team May 13, 2019 08:47 AM UTC

Hi Reihaneh, 

Greetings from Syncfusion. 

Introduced a new runtime permission model for the Android SDK version 23 and above. So, we need to include the following code for enabling the Android file provider to save and view the generated PDF document in Android.  

  1. Create a new XML file with the name of provider_paths.xml under the Android project Resources folder and add the following code in it.
Eg: Resources/xml/provider_paths.xml  
 
<?xml version="1.0" encoding="UTF-8" ?> 
  <external-path name="external_files" path="."/> 
</paths> 
 
  1. Add the following code to the AndroidManifest.xml file located under Properties/AndroidManifest.xml.

<?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. GettingStarted "> 
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="27" /> 
<application android:label=" GettingStarted.Android"> 
    <provider android:name="android.support.v4.content.FileProvider" 
          android:authorities="${applicationId}.provider" 
          android:exported="false" 
          android:grantUriPermissions="true"> 
      <meta-data android:name="android.support.FILE_PROVIDER_PATHS" 
                 android:resource="@xml/provider_paths" /> 
    </provider> 
  </application> 
</manifest> 
 
  1. Include the following code in the save file method of the Andoird project..

string extension = Android.Webkit.MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString()); 

string mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension); 

Intent intent = new Intent(Intent.ActionView); 

intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask); 

Android.Net.Uri path = FileProvider.GetUriForFile(Forms.Context, Android.App.Application.Context.PackageName + ".provider", file); 

intent.SetDataAndType(path, mimeType); 

intent.AddFlags(ActivityFlags.GrantReadUriPermission); 

Forms.Context.StartActivity(Intent.CreateChooser(intent, "Choose App")); 



To know more details, kindly refer the below KB link, 

Kindly try with the above solution in your end and let us know if it solves the issue.  

Regards, 
Sowmiya L  



RK Reihaneh Khaksaran May 14, 2019 01:08 PM UTC

Hi,

Thank you so much for your help it worked

Best regards,
Reihaneh


SL Sowmiya Loganathan Syncfusion Team May 15, 2019 05:48 AM UTC

Hi Reihanes, 
 
We glad to know that the issue is resolved. 
 
Regards, 
Sowmiya L 



PE peterV October 19, 2020 06:29 PM UTC

Hi, please help me
I repared project as you described but i get the error>

Severity Code Description Project File Line Suppression State
Error resource xml/provider_paths (aka com.companyname.GettingStarted:xml/provider_paths) not found. GettingStarted.Android C:\POkusy\GettingStarted\GettingStarted.Android\obj\Debug\90\android\manifest\AndroidManifest.xml 11



SL Sowmiya Loganathan Syncfusion Team October 20, 2020 11:57 AM UTC

Hi Peter,   
 
We have analyzed the reported issue. Can you please try the steps mentioned in the below KB and let us know the result,   
 
If you still facing the issue, please provide us the below details. It will helpful for us to provide the precise solution on this,   
 
  • Complete sample
  • Xamarin forms version
  • Android version
 
Regards,  
Sowmiya Loganathan 


Loader.
Live Chat Icon For mobile
Up arrow icon