On Android 11, intent to Gmail not attaching file. Says “Couldn't attach file”

Arvind Joshi 1 Reputation point
2021-03-16T20:35:33.487+00:00

My Xamarin Forms project is calling an intent to send a pdf. This is my code:
public void OpenShareIntent(string filePath)
{
var filepathSplit = filePath.Split('/');
var documentName = filepathSplit.Length > 1 ? filepathSplit[filepathSplit.Length - 1] : "InvalidPDF";
var externalFilePath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/" + documentName;
try
{
if (System.IO.File.Exists(externalFilePath))
{
System.IO.File.Delete(externalFilePath);
}
var fileBytes = File.ReadAllBytes(filePath);
File.WriteAllBytes(externalFilePath, fileBytes);
var intent = new Intent(Intent.ActionSend);
intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + externalFilePath));
intent.SetType("application/pdf");
MainActivity.CurrentActivity.StartActivity(Intent.CreateChooser(intent, "Share via"));
}
catch (UnauthorizedAccessException ex)
{
Logger.ReportException(ex);
}
catch (FileNotFoundException ex)
{
Logger.ReportException(ex);
}
}

When I select Gmail app I get the error "Couldn't attach file". Attachment works with Outlook app. My target framework is Android 10 and I am running my App on Samsung Galaxy S21.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
{count} votes