ITHCWY: Robert Ellison's Blog

Share a picture in MonoDroid

Here’s how to share a picture to Facebook, Twitter and so forth from MonoDroid:

Java.IO.File cache = ExternalCacheDir;
if ((cache == null) || (!cache.CanWrite()))
{
    // no external cache
    cache = CacheDir;
}

Java.IO.File tempFile = new Java.IO.File(cache, "temp.jpg");
using (FileStream fileStream = File.OpenWrite(tempFile.AbsolutePath))
{
    _currentBitmap.Compress(Bitmap.CompressFormat.Jpeg, 85, fileStream);
}

Intent shareIntent = new Intent(Intent.ActionSend);
shareIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(tempFile));
shareIntent.PutExtra(Intent.ExtraText, "Some text - appears in tweets, not on facebook"));
shareIntent.SetType("image/jpeg");

StartActivity(Intent.CreateChooser(shareIntent, "Share Image");

A fun mix of Java and C#. The directory got me to start with so check to see if the ExternalCacheDir is available and if not fall back to the internal CacheDir. Frustratingly Facebook doesn’t pick up on the text associated with an image regardless of the intent ExtraWhatever specified.

Comments

Kota Kira
Thank you for this.

Add Comment

All comments are moderated to weed out spam. Email address is optional and is only used to display your Gravatar.