question

22346713 avatar image
0 Votes"
22346713 asked JarvanZhang-MSFT edited

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(filename);
    
     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 doesn't work. May I know what is the problem? Or do I know how to send both at the same time.


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

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT edited

Hello @22346713 ,​

Welcome to our Microsoft Q&A platform!

Since Android 7.0, we need to use file provider to pass the file uri.

Context context = MainActivity.Instance;
...

var uri = AndroidX.Core.Content.FileProvider.GetUriForFile(context, context.PackageName + ".fileprovider", sharedFile);
intent.PutExtra(Intent.ExtraTitle, "Share Image");
intent.PutExtra(Intent.ExtraStream, uri);

context.StartActivity(Intent.CreateChooser(intent, "Share Image"));

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>

xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="external_files" path="."/>
  <root-path name="root-path" path="" />
</paths>


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.


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.