Xamarin.Form Share of Essentials image with URL

장주명 41 Reputation points
2021-09-15T11:16:14.097+00:00

I want to share the URL with the file and the message.

However, it seems that only one of the two can be shared using Share of Essentials.

I can implement both, but I don't know how to send both at the same time.

So I proceeded with the customization as follows.

Xamarin.From

public interface IShare
    {
        void Share(string subject, string message, byte[] image);
    }

private async void Button_Clicked(object sender, EventArgs e)
        {
            var imageStream = await ScreenShot.CaptureImageAsync();

            DependencyService.Get<IShare>().Share(" ", "Hello - https://www.naver.com/", GetImageBytes(imageStream));

        }

private byte[] GetImageBytes(Stream stream)
            {
                byte[] ImageBytes;
                using (var memoryStream = new System.IO.MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    ImageBytes = memoryStream.ToArray();
                }
                return ImageBytes;
            }

Android Code

public void Share(string subject, string message, byte[] image)
{
    ShareMultipleFilesRequest(message,image);
}

static Task ShareMultipleFilesRequest(string message, byte[] image)
{
    var intent = new Intent(Intent.ActionSend);

    intent.PutExtra(Intent.ExtraText, message);

    intent.SetType("image/png");
    intent.SetFlags(ActivityFlags.GrantReadUriPermission);

    string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    string filename = System.IO.Path.Combine(path, "CaptureScreen.png");

    System.IO.File.WriteAllBytes(filename, image);

    Java.IO.File sharedFile;

    sharedFile = new Java.IO.File(path);

    System.Console.WriteLine($"{sharedFile.IsAbsolute}");

    intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(new Java.IO.File(sharedFile.AbsolutePath)));

    intent.PutExtra(Intent.ExtraTitle, "Share Image");

    var chooserIntent = Intent.CreateChooser(intent, "Share Image" ?? string.Empty);

    var flags = ActivityFlags.ClearTop | ActivityFlags.NewTask;

    chooserIntent.SetFlags(flags);

    Xamarin.Essentials.Platform.AppContext.StartActivity(chooserIntent);

    return Task.CompletedTask;
}

iOS Code

 public void Share(string subject, string message, byte[] image)
        {

            var chartImage = new UIImage(NSData.FromArray(image));
            var mess = NSObject.FromObject(message);

            var activityItems = new[] { mess, chartImage };
            var activityController = new UIActivityViewController(activityItems, null);

            var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;

            while (topController.PresentedViewController != null)
            {
                topController = topController.PresentedViewController;
            }

            topController.PresentViewController(activityController, true, () => { });
        }

But Android Messages are shared. But the image is not shared. May I know what is the problem?

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