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
|
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.
|
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
|
if (Android.OS.Environment.IsExternalStorageEmulated)
{
root = Android.App.Application.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
}
else
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); |
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:
|
//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)); |