Based on @blackapps suggestions, I did the following changes.
1 - To add a new section in the AndroidManifest.xml
:
<provider android:name="androidx.core.content.FileProvider" android:authorities="tk.supernova.tmtimer.tk.supernova.tmtimer.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
</provider>
2 - To create a new file in the XML folder called file_paths that contains:
<?xml version="1.0" encoding="UTF-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="my_meetings" path="meetings/" />
</paths>
3 - And this changes to open and save the file correctly:
private File CreateDirFile(string fileName)
{
var meetingPath = new File(Xamarin.Essentials.FileSystem.AppDataDirectory, "meetings");
meetingPath.Mkdir();
var file = new File(meetingPath, fileName);
if (file.Exists())
{
file.Delete();
}
file.CreateNewFile();
return file;
}
private void Save(string fileName, string contentType, MemoryStream stream, Context context)
{
var file = CreateDirFile(fileName);
try
{
FileOutputStream outs = new FileOutputStream(file, false);
outs.Write(stream.ToArray());
outs.Flush();
outs.Close();
}
catch
{
Toast.MakeText(context, context.GetString(Resource.String.LblStorageIssue), ToastLength.Long).Show();
}
if (file.Exists() && contentType != APP_TYPE)
{
Intent intent = new Intent(Intent.ActionView);
if (Build.VERSION.SdkInt < BuildVersionCodes.N)
{
intent.SetDataAndType(Android.Net.Uri.FromFile(file), MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString())));
}
else
{
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
var contentUri = FileProvider.GetUriForFile(Application.Context, FILE_PROVIDER, file);
intent.SetDataAndType(contentUri, MimeTypeMap.Singleton.GetMimeTypeFromExtension(MimeTypeMap.GetFileExtensionFromUrl(Android.Net.Uri.FromFile(file).ToString())));
}
context.StartActivity(Intent.CreateChooser(intent, context.GetString(Resource.String.LblChooseApp)));
}
}
4 - I removed the strict section.