1

I am using this code for sharing text + message in android. This method works fine for g+ or email, but dont work for Facebook. It says cant load file.

public void Share(string title, string content)
    {
        if (ActivityContext.Current == null || Application.Context == null)
            return;

        if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(content))
            return;

        Bitmap bitmapToShare = BitmapFactory.DecodeResource(Application.Context.Resources, Resource.Drawable.icon_120);
        File pictureStorage = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
        File noMedia = new File(pictureStorage, ".nomedia");
        if (!noMedia.Exists())
            noMedia.Mkdirs();
        string imageName = "shared_image" + DateTime.Now.ToBinary() + ".png";
        File file = new File(noMedia, imageName);
        Stream output = Application.Context.OpenFileOutput(imageName,FileCreationMode.WorldReadable);
        bitmapToShare.Compress(Bitmap.CompressFormat.Png, 100, output);
        output.Flush();
        output.Close();

        // var name = Application.Context.Resources.GetResourceName(Resource.Drawable.icon_120).Replace(':', '/');
        // var imageUri = Uri.Parse("android.resource://" + name);
        var sharingIntent = new Intent();
        sharingIntent.SetAction(Intent.ActionSend);
        sharingIntent.SetType("text/plain");
        sharingIntent.SetType("image/png");
        sharingIntent.PutExtra(Intent.ExtraTitle, title);
        sharingIntent.PutExtra(Intent.ExtraText, content);
        // sharingIntent.PutExtra(Intent.ExtraStream, imageUri);
        sharingIntent.PutExtra(Intent.ExtraStream, Uri.FromFile(file));
        sharingIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
        ActivityContext.Current.StartActivity(Intent.CreateChooser(sharingIntent, title));

        AnalyticsService.Instance.LogEvent("Share button clicked.");
    }

Tried this post. Am i doing something wrong?

Community
  • 1
  • 1
F0rc0sigan
  • 676
  • 2
  • 11
  • 22

0 Answers0