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.
- SaveFile() – that accept the file name and the file stream as byte[] and return file path.
- 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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Android Specific Code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
How to use this, please can you explain it in brief
LikeLiked by 1 person
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;
}
}
LikeLiked by 1 person