【问题标题】:Xamarin.iOS get image filepath for attaching in emailXamarin.iOS 获取附加在电子邮件中的图像文件路径
【发布时间】:2021-01-06 18:21:51
【问题描述】:

我有一种方法可以获取之前下载到手机图库中的图像并将其附加到电子邮件中。这在 Android 中完美运行。但在 iOS 中,我无法弄清楚如何获取我刚刚保存到图库中的图像的文件路径。

这是目前的 Xamarin.IOS 代码:

public string SaveToGallery(byte[] imgBytes, string fileName)       
        {
            var imageData = new UIImage(NSData.FromArray(imgBytes));
            string filePath = string.Empty;
            NSError errorMsg = null;

            imageData.SaveToPhotosAlbum((image, error) =>
            { 
                if (error != null)
                    errorMsg = error;           
            });

            if (errorMsg is null)
            {
                return string.Empty;
            }
                
            return filePath;
        }

我使用 Xamarin.Essentials.Email 创建电子邮件,EmailAttachment 类将字符串 fullPath 作为参数。 SaveImg() 方法使用 DependencyService 调用上面保存图像的 Xamarin.iOS 代码,并应返回保存图像的文件路径:

private static async Task EmailImg(List<string> recipients, string subject, string body, byte[] imgBytes)
        {
            try
            {
                var message = new EmailMessage
                {
                    To = recipients,
                    Subject = subject,
                    Body = body,
                    //Cc = ccRecipients,
                    //Bcc = bccRecipients
                };

                string filePath = string.Empty;
                if (imgBytes != null)
                {
                    filePath = await SaveImg(imgBytes);                 

                    if (!string.IsNullOrEmpty(filePath))
                        message.Attachments.Add(new EmailAttachment(filePath));
                }

                await Email.ComposeAsync(message);
            }
            catch (FeatureNotSupportedException fbsEx)
            {
                await Application.Current.MainPage.DisplayAlert("Oops", "Email function not supported on this device", "Okay");
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Oops", "Failed to pass details to email client", "Okay");
            }
        }

【问题讨论】:

    标签: c# xamarin xamarin.ios


    【解决方案1】:

    您不需要将图片保存到图库。您可以将它们保存到本地文件夹并使用该本地 filePath 添加附件。 (iOS 和 Android 都可以使用此方法)

    示例如下:

    private async void EmailImg(List<string> recipients, string subject, string body, byte[] imgBytes)
    {
        try
        {
            var message = new EmailMessage
            {
                To = recipients,
                Subject = subject,
                Body = body,
                //Cc = ccRecipients,
                //Bcc = bccRecipients
            };
    
            var fileName = "myImage.jpg";
            var filePath = Path.Combine(FileSystem.AppDataDirectory, fileName);
    
            File.WriteAllBytes(filePath, imgBytes);
    
            if (imgBytes != null)
            {
                if (!string.IsNullOrEmpty(filePath))
                    message.Attachments.Add(new EmailAttachment(filePath));
            }
    
            await Email.ComposeAsync(message);
        }
        catch (FeatureNotSupportedException fbsEx)
        {
            await Application.Current.MainPage.DisplayAlert("Oops", "Email function not supported on this device", "Okay");
        }
        catch (Exception ex)
        {
            await Application.Current.MainPage.DisplayAlert("Oops", "Failed to pass details to email client", "Okay");
        }
    }
    

    如果将图片保存到 iOS 库中,此时无法获取路径。只有选择路径才能得到路径,看这个。

    【讨论】:

      猜你喜欢
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多