1

Quiero poder enviar un archivo de tipo Image, por WhatsApp, desde Xamarin Android, hasta ahorita sólo pude enviar Texto, si alguien pudiera ayudarme, o darme un enlace con algo de documentación para eso, busqué pero no logre encontrar mucho en C#, ésto es lo que tengo para el envío de texto

#region WhatsApp
string whatsapp = "com.whatsapp";
        bool VerificarApp(String uri)
        {
            try
            {
                ApplicationContext.PackageManager.GetPackageInfo(uri, PackageInfoFlags.Activities);
                return true;
            }
            catch (PackageManager.NameNotFoundException e)
            {
                return false;
            }
        }

        public void EnviarWhatsApp()
        {
            if (VerificarApp(whatsapp))
            {
                Intent intent = new Intent();
                intent.SetAction(Intent.ActionSend);
                intent.PutExtra(Intent.ExtraText, "Prueba de envío de WhatsApp desde Xamarin Android, Oscar N.");
                intent.SetType("text/plain");
                //intent.SetType("image");
                intent.SetPackage(whatsapp);
                StartActivity(intent);
            }
            else
            {
                Toast.MakeText(this, "WhatsApp no está instalado. No se pudo enviar el mensaje", ToastLength.Long).Show();
            }
        }

        #endregion

1 respuesta 1

2

Ya pude hacerlo ;)

public void SharedImageSocialNetworks(string title, string content)
            {
                try
                {
                    if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(content))
                        return;

                    Bitmap b = BitmapFactory.DecodeResource(Resources, Resource.Drawable.halo);
                    var tempFilename = "Test.png";
                    var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
                    var filePath = System.IO.Path.Combine(sdCardPath, tempFilename);
                    using (var os = new FileStream(filePath, FileMode.Create))
                    {
                        b.Compress(Bitmap.CompressFormat.Png, 100, os);
                    }
                    b.Dispose();
                    var imageUri = Android.Net.Uri.Parse($"file://{sdCardPath}/{tempFilename}");
                    var intent = new Intent();
                    intent.SetAction(Intent.ActionSend);
                    intent.SetType("image/*");
                    intent.PutExtra(Intent.ExtraText, content);
                    intent.PutExtra(Intent.ExtraStream, imageUri);
                    intent.AddFlags(ActivityFlags.GrantReadUriPermission);
                    StartActivity(Intent.CreateChooser(intent, title));
                }
                catch (Exception ex)
                {
                    Toast.MakeText(this, "Error: " + ex.Message, ToastLength.Short).Show();
                }
            }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.