1

I have a method to be able to share images through SMS, WhatsApp, Email, Fb, etc., to which when calling it a URL is passed.

The method downloads the image from the shared URL and the image is ready to send (Xamarin.Essentials.Share is used).

The problem is that I pass a text along with the image (need to give some context) as part of the method property, but the text does not show it to me at the time of sharing, only the image alone, and it does not serve me well since it would send it without any context or information.

Any other ideas on how to pass the image and a text on Android?

Maybe some kind of automatic copy and then paste some text to the keyboard clipboard?

Image and Text Method:

public async Task DownloadImageAndShareIt(string URL)
        {
            try
            {
                string localPath = "";

                var webClient = new WebClient();
                webClient.DownloadDataCompleted += (s, e) =>
                {
                    byte[] bytes = new byte[e.Result.Length];
                    bytes = e.Result; // get the downloaded data
                    string documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory
                    (Android.OS.Environment.DirectoryPictures).AbsolutePath;

                    var partedURL = URL.Split('/');
                    string localFilename = partedURL[partedURL.Length - 1];
                    localFilename = "MyAPP" + localFilename;
                    localPath = System.IO.Path.Combine(documentsPath, localFilename);
                    File.WriteAllBytes(localPath, bytes); // writes to local storage

                    MediaScannerConnection.ScanFile(Application.Context, new string[] { localPath }, null, null);
                };
                var url = new Uri(URL);
                webClient.DownloadDataAsync(url);

                var partedURL = URL.Split('/');
                string localFilename = partedURL[partedURL.Length - 1];
                localFilename = "MyAPP" + localFilename;
                string documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory
                    (Android.OS.Environment.DirectoryPictures).AbsolutePath;
                localPath = System.IO.Path.Combine(documentsPath, localFilename);

                //Done.



            }
            catch (Exception Ex)
            {
                string LineErrorNumber = "Error line: " + Ex.StackTrace.Substring(Ex.StackTrace.Length - 7, 7) + "\r\n" + "Error: " + Ex.Message;
            }
            finally
            {
                await Share.RequestAsync(new ShareFileRequest
                {
                    Title = **"Delicate info from MyAPP"**,
                    File = new ShareFile(localPath)
                });
            }
        }

Error

It should be noted that if I use another Share method that is only for text, there if I share it without problems.

Text only Method:

private async Task ShareText(string Tipo, string Titulo, string ContenidoaCompartir)
        {
            try
            {
                await Share.RequestAsync(new ShareTextRequest
                {
                    Uri = "Delicate info from MyAPP",
                    Title = Titulo, 
                    Subject = (Tipo + " de " + Titulo).ToString(),
                    Text = "MyApp - " + Tipo + " de " + Titulo + ":" + System.Environment.NewLine + ContenidoaCompartir + System.Environment.NewLine
                });
            }
            catch (Exception Ex)
            {
                string LineErrorNumber = "Error line: " + Ex.StackTrace.Substring(Ex.StackTrace.Length - 7, 7) + "\r\n" + "Error: " + Ex.Message; Crashes.TrackError(Ex);
            }
        }
  • have you try set some breakpoints to check the value and to see how far the method goes – Adrain Nov 15 '21 at 09:17
  • 1
    Hey Adrain. Yes, I did and I found out about this: It does not show the parameter ´title´ because an SMS or a WhatsApp Text does not have a "Title". And it works with the text-only method because it has the 'Text' parameter as well as the SMS. Now with that said, I still need to find a way to do this. – Joel Hernández Nov 15 '21 at 16:08
  • have you seen this? https://stackoverflow.com/questions/42900244/how-can-i-share-text-and-image-in-android – Adrain Nov 16 '21 at 09:19
  • @AdrainZhu-MSFT Not quite what I need. The result It's basically the same as the Text Method and just passing the URL in the `text` parameter. – Joel Hernández Nov 16 '21 at 15:32
  • 1
    here is sth about share text and image through whatsapp https://stackoverflow.com/questions/23077338/share-image-and-text-through-whatsapp-or-facebook – Adrain Nov 17 '21 at 06:54
  • @ AdrainZhu-MSFT Cool!! I half tested the Whatsapp method and apparently it works (half tested because it opens the Whatsapp application but it is not configured on the virtual device yet, I need to test it on a physical device). You should add this as an answer because there are only 5 days left, the weekend is coming and I think yours will be the only answer (if you are interested in the points I mean). – Joel Hernández Nov 17 '21 at 18:29

1 Answers1

1

You can check the question below which shows how to share text and image through whatsapp Share image and text through Whatsapp or Facebook

Adrain
  • 1,946
  • 1
  • 3
  • 7