question

22346713 avatar image
0 Votes"
22346713 asked KyleWang-MSFT commented

Xamarin.Form Share of Essentials image with URL


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?


dotnet-xamarinformsdotnet-xamarinessentials
· 5
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.

Have you checked out the Share API in Xamarin.Essentials:

https://docs.microsoft.com/xamarin/essentials/share?tabs=ios&amp;WT.mc_id=friends-0000-jamont#file

This will handle it all for you automatically.

0 Votes 0 ·
22346713 avatar image 22346713 JamesMontemagno ·

Oh, it's very nice to meet you. I studied a lot while watching your video. It's only been three months since I started xamarin. So I looked at the API code and copied it, but I couldn't copy it the same because there was an internal access restriction. So is there a way to run ShareTextRequest and ShareFileRequest at the same time with RequestAsync or send URI and image at the same time?

0 Votes 0 ·
22346713 avatar image 22346713 22346713 ·

This works very well without requiring authority. Then I can't add url. What should I do?

 private async void Button_Clicked_1(object sender, EventArgs e)
         {
             var imageStream = await ScreenShot.CaptureImageAsync();
    
             string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
             string filename = Path.Combine(path, "CaptureScreen.png");
    
             File.WriteAllBytes(filename, GetImageBytes(imageStream));
    
             Console.WriteLine(path);
             Console.WriteLine(filename);
    
             await Share.RequestAsync(new ShareFileRequest
             {
                 Title = "share",
                 File = new ShareFile(filename)
             });
         }

0 Votes 0 ·

@22346713 For this, you can submit an issue/feature request on GitHub: xamarin/Xamarin.Forms.

0 Votes 0 ·

0 Answers