Share Bitmap in Xamarin Android

Alessio Camaroto 81 Reputation points
2021-06-28T06:34:57.507+00:00

I'm trying to share a bitmap, for example by mail, but nothing is shared (mail without bitmap). No exception and no crash in my app. App is being developed in Xamarin.Android and I test it on Android 11 device and emulator. Bitmap is correctly stored and it is not empty.

public void Share(string filename, string title, string content)
{
if (string.IsNullOrEmpty(filename) || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(content)) return;
var imageView = FindViewById<ImageView>(Resource.Id.generatedImage);
Android.Graphics.Drawables.BitmapDrawable bitmapDrawable = ((Android.Graphics.Drawables.BitmapDrawable)imageView.Drawable);
if (bitmapDrawable != null)
{
Bitmap bitmap = bitmapDrawable.Bitmap;
string testPath = Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath + Java.IO.File.Separator + $"{filename}.png";
using (System.IO.FileStream os = new System.IO.FileStream(testPath, System.IO.FileMode.Create))
{
bitmap.Compress(Bitmap.CompressFormat.Png, 100, os);
os.Close();
}
var sharingIntent = new Intent();
sharingIntent.SetAction(Intent.ActionSend);
sharingIntent.SetType("image/*");
sharingIntent.PutExtra(Intent.ExtraSubject, Resources.GetString(Resource.String.contact_subject));
sharingIntent.PutExtra(Intent.ExtraText, content);
sharingIntent.PutExtra(Intent.ExtraStream, testPath);
StartActivity(Intent.CreateChooser(sharingIntent, title));
}
}

This is my AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android:requestLegacyExternalStorage="true"

Thanks
Alessio

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

Accepted answer
  1. JarvanZhang 23,936 Reputation points
    2021-06-29T08:27:51.853+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    According to the posted code, you seem not to attach the file when sending the email. You need to pass the file uri instead of the file name.

       //create the file with the path first  
       var uri = FileProvider.GetUriForFile(this, PackageName + ".fileprovider", file);  
         
       var sharingIntent = new Intent();  
       sharingIntent.SetAction(Intent.ActionSend);  
       sharingIntent.SetType("image/*");  
       sharingIntent.PutExtra(Intent.ExtraSubject, "contact_subject");  
       sharingIntent.PutExtra(Intent.ExtraText, content);  
       sharingIntent.PutExtra(Intent.ExtraStream, uri);  
       StartActivity(Intent.CreateChooser(sharingIntent, title));  
    

    It requires to config the FileProvider in AndroidManifest.xml.

       <provider android:name="androidx.core.content.FileProvider"   
                 android:authorities="${applicationId}.fileprovider"   
                 android:exported="false"   
                 android:grantUriPermissions="true">  
                   
             <meta-data android:name="android.support.FILE_PROVIDER_PATHS"   
                            android:resource="@xml/file_paths"></meta-data>  
       </provider>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Alessio Camaroto 81 Reputation points
    2021-07-03T09:09:37.48+00:00

    It works thanks a lot.

    0 comments No comments