1

I am using Xamarin.Forms PCL project and I am using the SignaturePad plugin to capture the signature and I am saving in memory stream like:

var imageStream = await 
signaturePad.GetImageStreamAsync(SignatureImageFormat.Png);
var signatureMemoryStream = new MemoryStream();
imageStream.CopyTo(signatureMemoryStream);
byte[] data = signatureMemoryStream.ToArray();

Now, what I need to do is attach that signature image in an email. So for that, I am using the Cross-Messaging NuGet plugin which allows me to attach the image inside an email. So, I need a path of the Signature image. So any idea, how can I save the signature in my local Xamarin PCL shared project path and get the path for the image?

Kartik Solanki
  • 161
  • 1
  • 10
  • 1
    SignaturePad doesn't expose a path property. You will need to write the bitmap to disk yourself – Jason Jan 17 '18 at 15:19

1 Answers1

0

Saving a file to the device is device specific, so you can't do it in the PCL directly. What you can do is use dependency injection to call into your Android and iOS projects to perform the save and return the title. So from your code you would call:

var filePath = DependencyService.Get<IPlatform>().SaveFile(fileBytes, fileName);

which is defined in your PCL:

public interface IPlatform
{
    string SaveAndOpenFile(byte[] fileBytes, string fileName);
    void DownloadFile(string url);
}

Then in your android project you would do something like...:

[assembly: Xamarin.Forms.Dependency(typeof(Platform))]

namespace MyApp.Droid.Classes
{
    internal class Platform : IPlatform
    {
    public async void SaveAndOpenFile(byte[] fileBytes, string fileName)
    {
        var filePath = string.Empty;
        try
        {
            // Write File
            var directory = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            directory = Path.Combine(directory, Android.OS.Environment.DirectoryDownloads);
            filePath = Path.Combine(directory, fileName);
            File.WriteAllBytes(filePath, fileBytes);
            return filePath
        }
    }
        }
    }

and similarly in iOS:

[assembly: Xamarin.Forms.Dependency(typeof(Platform))]

namespace MyApp.iOS.Classes
{
internal class Platform : IPlatform
{
    public void SaveAndOpenFile(byte[] fileBytes, string fileName)
    {
    }
  }
}
Christine
  • 562
  • 3
  • 19
  • In iOS , it is saying 'Platform' does not implement interface member 'IPlatform.SaveAndOpenFile(byte[]),String)' Also, any idea how should I write code inside SaveAndOpenFile method ??? – Kartik Solanki Jan 18 '18 at 17:26
  • I'm guessing it's something like: var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var filePath = Path.Combine(directory, fileName); File.WriteAllBytes(filePath, fileBytes); but I haven't tested it. You could let VS create the function definition in iOS and see what is different. – Christine Jan 19 '18 at 18:15