question

ArvindJoshi-3238 avatar image
0 Votes"
ArvindJoshi-3238 asked RobCaplan edited

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

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.











dotnet-xamarin
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,ArvindJoshi-3238. Try using provider to get the uri of the attach file, here is a similar case you could refer to:
https://stackoverflow.com/questions/65463541/attaching-cache-file-to-gmail-through-fileprovider-and-intent-not-working

And you could try using the Xamarin.Essentials.Email api to send the email with attachment.


0 Votes 0 ·

Hi JarvanZhang-MSFT,
I am already using FileProvider. The functionality is still not working.

Some of the other programs are working, e.g. using Outlook to send e-mail or saving file to GoogleDrive etc.

0 Votes 0 ·

I am already using FileProvider. The functionality is still not working.

Any error occured when using FileProvider? Will the code work on Android 10? Android 11 introduces changes related to package visibility. Check the doc: https://developer.android.com/about/versions/11/privacy/package-visibility

0 Votes 0 ·

0 Answers