Share <Image> Source using Essentials' Share

Jassim Al Rahma 1,526 Reputation points
2021-05-07T22:04:21.17+00:00

Hi

How can I Share <Image>'s Source using Xamarin Essentials Share?

Thanks,
Jassim

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

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,751 Reputation points
    2021-05-11T07:02:51.96+00:00

    Hello,

    Welcome to Microsoft Q&A!

    It depends on where the image comes from .

    • If the image is available on internet , we can just share the link . await Share.RequestAsync(new ShareTextRequest
      {
      Uri = uri,
      Title = "Image Link"
      });
      • If the image is from internal storage , we can share the file according to the path. await Share.RequestAsync(new ShareFileRequest
        {
        Title = Title,
        File = new ShareFile(file)
        });
      • If the image is from Embedded resource(shared project ) , we can get the stream from the file and save it into file , and then share the file . //read stream from bundle
        var assembly = IntrospectionExtensions.GetTypeInfo(typeof(App)).Assembly;
        Stream stream = assembly.GetManifestResourceStream("FormsA.dog2.png"); //save data into file
        var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        documentsPath = Path.Combine(documentsPath, "Share");
        Directory.CreateDirectory(documentsPath); string filePath = Path.Combine(documentsPath, "share.png"); byte[] bArray = new byte[stream.Length];
        using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
        {
        using (stream)
        {
        stream.Read(bArray, 0, (int)stream.Length);
        }
        int length = bArray.Length;
        fs.Write(bArray, 0, length);
        } //share from file
        await Share.RequestAsync(new ShareFileRequest
        {
        Title = Title,
        File = new ShareFile(filePath)
        });

    Best Regards,
    Cole Xia


    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.