Cross Platform Utility for saving images in SQLite.

xamarinandsqlite

On my way to developing a cross-platform application using Xamarin.Forms .It comes to a point when I need to fetch all the images of the products from the REST API and save it into my application database or SQLite. Also, I want the path of the image location to be returned back on successful saving so that I can save it into my product table and make use of it whenever I want to display image of the product.

I had gone through a lot of articles but won’t find an appropriate solution to achieve what I want. Finally, I have decided to develop my own utility which will help me to achieve this.And I would be glad to share my solution with you all.

My plan for the solution:
I extract an interface for the utility which will be in PCL and then create platform specific implementation of that. The interface contains two methods.

  1. SaveFile() – that accept the file name and the file stream as byte[] and return file path.
  2. DeleteDirectory() – that will delete existing folder ever a time when I sync the data from my API.

Now, lets take look on the code.

Common Interface:


public interface IFileUtility
{
/// <summary>
/// Use to save file in device specific folders
/// </summary>
/// <param name="fileName"></param>
/// <param name="fileStream"></param>
/// <returns></returns>
string SaveFile(string fileName,byte[] fileStream);
/// <summary>
/// Used to delete the existing file directory, before syncing the file again.
/// </summary>
void DeleteDirectory();
}

view raw

IFileUtility.cs

hosted with ❤ by GitHub

Android Specific Code:


public class FileUtility : IFileUtility
{
public string SaveFile(string fileName,byte[] imageStream)
{
string path = null;
string imageFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ProductImages");
//Check if the folder exist or not
if (!System.IO.Directory.Exists(imageFolderPath))
{
System.IO.Directory.CreateDirectory(imageFolderPath);
}
string imagefilePath = System.IO.Path.Combine(imageFolderPath, fileName);
//Try to write the file bytes to the specified location.
try
{
System.IO.File.WriteAllBytes(imagefilePath, imageStream);
path = imagefilePath;
}
catch (System.Exception e)
{
throw e;
}
return path;
}
public void DeleteDirectory()
{
string imageFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ProductImages");
if (System.IO.Directory.Exists(imageFolderPath))
{
System.IO.Directory.Delete(imageFolderPath,true);
}
}
}

iOS Specific Code:


public class FileUtility : IFileUtility
{
public string SaveFile(string fileName,byte[] fileStream)
{
string path = null;
string imageFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ProductImages");
//Check if the folder exist or not
if (!System.IO.Directory.Exists(imageFolderPath))
{
System.IO.Directory.CreateDirectory(imageFolderPath);
}
string imagefilePath = System.IO.Path.Combine(imageFolderPath, fileName);
//Try to write the file bytes to the specified location.
try
{
System.IO.File.WriteAllBytes(imagefilePath, fileStream);
path = imagefilePath;
}
catch (System.Exception e)
{
throw e;
}
return path;
}
public void DeleteDirectory()
{
string imageFolderPath = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.Personal), "ProductImages");
if (System.IO.Directory.Exists(imageFolderPath))
{
System.IO.Directory.Delete(imageFolderPath,true);
}
}
}

2 thoughts on “Cross Platform Utility for saving images in SQLite.

    1. You just need to inject object of IFileUtility into the constructor of your class.

      public Class Demo()
      {
      IFileUtility _fileUtility;

      public Demo(IFileUtility fileUtility)
      {
      _fileUtility = fileUtility;
      }
      }

      Liked by 1 person

Leave a comment