Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

c# - Xamarin.iOS get image filepath for attaching in email

I have a method that takes an image that was previously downloaded to the phones gallery and attaches it to an email. This works perfectly in Android. But In iOS I can't figure out how to get the file path of the image that I literally just saved to the gallery.

Here is the Xamarin.IOS code so far:

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

I'm using Xamarin.Essentials.Email to create the email, the EmailAttachment class takes string fullPath as the parameter. The SaveImg() method uses DependencyService to call the Xamarin.iOS code above which saves the image and should return the file path of where the image was saved:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You don't need to save the image to gallery. You can save them to the local folder and use that local filePath to add the Attachment. (You can use this method for both iOS and Android)

Here is the example:

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

If you save the image to iOS gallery, you can't get the path at that time. You can only get the path when you pick it, see this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...