Home > database >  Android Share method does not share text and image, only the image - Android
Android Share method does not share text and image, only the image - Android

Time:11-20

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);
            }
        }

CodePudding user response:

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

  • Related