Can't save and view PDF on Xamarin Forms 5 and AndroidX

I've downloaded th esample and it works it shows me the created PDF, but I've noticed that the sample is on Xamarin Forms 3 and Android, my project is on Xamarin Forms 5 and AndroidX, library Android.Support.V4.Contetn is not available there, so I have to use AndroidX library, but the problem is also with Manifest it works for example with that

<?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="29" />

<application android:label="GettingStarted.Android" android:requestLegacyExternalStorage="true">

<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>



but I tried it with my program and still get error that cannot find resource, and without modifying Manifest it just doesn't save and show the PDF. this is my Manifest

<?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.app2">

    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />

    <application android:label="App2.Android" android:theme="@style/MainTheme">

<provider

   android:name="androidx.core.content.FileProvider"

   android:authorities="com.companyname.App2"

   android:exported="false"

   android:grantUriPermissions="true">

<meta-data

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/file_paths" />

</provider>

</application>

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>

Can you help me? :D


5 Replies

GK Gowthamraj Kumar Syncfusion Team March 15, 2022 12:00 PM UTC

Hi Mateusz, 
 
We have created a sample to write the PDF document in Android 11 and attached it for your reference. Kindly try the below working sample on your end and let us know whether it works or not. 
In this sample, we have modified the following, 
  1. User permission for read, write external storage.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">               
</uses-permission>   
            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />   
  1. Replaced the following code to get the external storage download folder in the SaveAndroid.cs file.
if (Android.OS.Environment.IsExternalStorageEmulated) 
{ 
root = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath; 
} 
else 
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 

Please refer to the below link for more information,
UG: https://help.syncfusion.com/file-formats/pdf/create-pdf-file-in-xamarin
 
 
Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 



MA Mateusz March 28, 2022 11:32 AM UTC

Hi,

I am testing that for few days, the sample works and my project works if I copy my files to the sample project, but still cannot change it in my project. Could you write what is necessary to do, I mean do I have to add references etc. When I add this metadata line in Manifest that is in the sample I still get errors.


Probably I just miss something in my project.

But what I mean could you write what do I have to add after creating clear Xamarin Forms project?

BR

Mateusz



SV Surya Venkatesan Syncfusion Team replied to Mateusz March 29, 2022 12:57 PM UTC

Hi Mateusz,


We recommend you refer to the following KB documentation: https://www.syncfusion.com/kb/9174/how-to-create-a-pdf-file-in-xamarin up to seventh if you are using Android-X. From the eighth step: Download the helper files from this link and add them into the mentioned project. we request you to replace the previously updated sample contain manifest details and replace the SaveAndroid.cs file.


Note: the Android.OS.Environment.GetExternalStoragePublicDirectory API is deprecated in Android 11. So, We have found the alternative solution to overcome this API issue by using GetExternalFilesDir() method instead of Environment.GetExternalStoragePublicDirectory () to save the PDF document. Kindly refer to the following code snippet,

if (Android.OS.Environment.IsExternalStorageEmulated) 

{ 

root = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath; 

} 

else 

root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 

 

If the same issue persists, Kindly revert us with the issue caused sample to replicate the issue and provide the solution at the earliest with precise guidelines to overcome that issue.


Regards,

Surya V



MA Mateusz March 29, 2022 06:16 PM UTC

Hi,

I did it, but still in SaveAndroid I had to add using for 

using AndroidX.Core.Content;

using AndroidX.Core.App;

and it still can't see 

using Android.Support.V4.Content;


and still the same error in AndroidManifest:

Error resource xml/provider_paths (aka com.companyname.appname:xml/provider_paths) not found.



GK Gowthamraj Kumar Syncfusion Team March 30, 2022 03:33 PM UTC

Hi Mateusz,

The reported error may due the particular xml file is not present in your application. Please make sure that the provider_paths.xml under the Android project resource folder on your end or not?


And ensure 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.mycompany.pdfxamarinsample" android:installLocation="auto">

                                                                                                                      <uses-sdk android:minSdkVersion="30" android:targetSdkVersion="30" />

                                                                                                                      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">                                                                                                                                 

                                                                                                                      </uses-permission>

                                                                                                                      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

                                                                                                                      <uses-permission android:name="android.permission.INTERNET" />

                                                                                                                      <application android:label=" PDFXamarinSample.Android" android:requestLegacyExternalStorage="true">

                                                                                                                                                                                                                                            <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>


Please refer the below dependencies reference details for Android project to get all Android namespace,


Please let us know if you need any further assistance with this.


Regards,

Gowthamraj K


Loader.
Up arrow icon