PDF control Obsolete code "SaveAndroid.cs" for android

I am using the control to generate a PDF and it tells me that some things are obsolete in the code


if (ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.WriteExternalStorage) != Permission.Granted) {

 ActivityCompat.RequestPermissions((Android.App.Activity)Forms.Context, new String[] { Manifest.Permission.WriteExternalStorage }, 1); 

 }

Forms.Context obsolete


and

//Get the root path in android device.

        if (Android.OS.Environment.IsExternalStorageEmulated)

        {

            root = Android.OS.Environment.ExternalStorageDirectory.ToString();

        }

        else

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


ExternalStorageDirectory Obsolete


8 Replies

GK Gowthamraj Kumar Syncfusion Team November 1, 2021 12:34 PM UTC

Hi eyner, 
 
Thank you for contacting Syncfusion support. 
 
if (ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.WriteExternalStorage) != Permission.Granted) { 
 ActivityCompat.RequestPermissions((Android.App.Activity)Forms.Context, new String[] { Manifest.Permission.WriteExternalStorage }, 1);  
 } 
Forms.Context obsolete 
 
This API is deprecated from Xamarin Forms API level 29. We have alternative solution to overcome this depreciated APIs by changing the Forms.Context to Android.App.Application.Context. 
Get the root path in android device. 
        if (Android.OS.Environment.IsExternalStorageEmulated) 
        { 
            root = Android.OS.Environment.ExternalStorageDirectory.ToString(); 
        } 
        else 
            root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
 
ExternalStorageDirectory Obsolete 
 
You can replace the following code to get the external storage download folder in the SaveAndroid.cs file. 
if (Android.OS.Environment.IsExternalStorageEmulated)  
  {  
root = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;              
  }  
 
 
 
 
Regards, 
Gowthamraj K 



EY eyner November 3, 2021 08:19 AM UTC

Thanks for answering.

It tells me that it is also obsolete and that I do not have permission to access the path

root = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath

Deprecated


the project is for

android: minSdkVersion = "21"

android: targetSdkVersion = "30"


And for this piece of code which is the permission I had to use a plugin to make it work

"Plugin.CurrentActivity;"


Before

if (ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.WriteExternalStorage) != Permission.Granted) {

ActivityCompat.RequestPermissions((Android.App.Activity)Forms.Context, new String[] { Manifest.Permission.WriteExternalStorage }, 1);

}

After

using Plugin.CurrentActivity;

var activity = CrossCurrentActivity.Current.Activity;

if (ContextCompat.CheckSelfPermission(Android.App.Application.Context, Manifest.Permission.WriteExternalStorage) != Permission.Granted)

{

ActivityCompat.RequestPermissions(activity, new[]

{

Manifest.Permission.WriteExternalStorage

}, 1);

}


Could you make a sample if possible please



PV Prakash Viswanathan Syncfusion Team November 4, 2021 11:08 AM UTC

Hi Eyner, 

Thank you for the update.  

Currently we are creating sample with alternate APIs for absolute APIs. We will update you the working sample by Nov 9, 2021.  

Regards, 
Prakash V 



SV Surya Venkatesan Syncfusion Team November 9, 2021 01:51 PM UTC

Hi Eyner, 
 
Thank you for your patience.  
 
Still we are working on your requirement and we will update the further details on November 11th 2021. 
 
Regards, 
Surya V 



EY eyner replied to Surya Venkatesan November 9, 2021 10:13 PM UTC

Thanks i waiting



GK Gowthamraj Kumar Syncfusion Team November 11, 2021 05:02 PM UTC

Hi eyner, 
 
Thank you for your patience. 
 
On our further analysis, the Android.OS.Environment.GetExternalStoragePublicDirectory API is deprecated in Android 11. We have found the alternative solution to overcome this API issue by using GetExternalFilesDir() method instead of Environment.GetExternalStoragePublicDirectory () to get the external file directory from Application context. 
 
Please find the below 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); 
   
We have attached the runnable sample for your reference, please find the sample from below link,
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/PDFXamarinSample-1531951196
 
 
Please let us know if you need any further assistance in this. 
 
Regards, 
Gowthamraj K 



EY eyner November 12, 2021 12:53 PM UTC

Its works thanks you.

I have one last question, I have been trying to add images but I get this exception, I don't understand very well in which project or folder the images should be added and how to call them to place them in the PDF document.


this is what I have tried:

exception2.JPG




SV Surya Venkatesan Syncfusion Team November 15, 2021 01:35 PM UTC

Hi eyner, 
 
Thank you for your update. 
 
We have checked the provided code snippet for inserting images in PDF document using Xamarin. We suspect that the exception occurs due to incorrect image path while getting the image stream. We are recommended you to get the image(external resources) file from Assets folder of main project. And we need to set Build Action to Embedded resource for external resource such as images, pdf etc., 
 
Please try the below modified code snippet to get the image file. 
//Get the images as stream 
Stream imageStream =  typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("PDFejemplo.Assets.Logo.png"); 
  
//Create a new PdfBitmap instance 
PdfBitmap image = new PdfBitmap(imageStream); 
  
//Draw the image 
loadedPage.Graphics.DrawImage(image, new PointF(40, 100)); 
 
Please refer the 7th step in the below KB documentation, 
 
Please try the above suggestion on your end and lets know the result. 
 
Regards, 
Surya V 


Loader.
Up arrow icon