I want to store images path in sqlite database in xamarin forms for cross platform application. How to store path of image??
Asked
Active
Viewed 5,374 times
0
-
2Possible duplicate of [How to store image in SQLite database](http://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database) – SushiHangover Sep 14 '16 at 05:17
-
Show the code that you have tried so far : http://stackoverflow.com/help/how-to-ask – SushiHangover Sep 14 '16 at 05:18
-
I do not know how to do it in xamarin forms and even not getting any example for start to code so how could i write code without any information about images storage . Now, I am saving only other simple information in database like name, price etc. – Karan Sep 14 '16 at 05:24
-
https://developer.xamarin.com/guides/xamarin-forms/working-with/databases/ – SushiHangover Sep 14 '16 at 05:26
-
https://www.google.com/search?btnG=1&pws=0&q=c%23+sqlite+save+bitmap – SushiHangover Sep 14 '16 at 05:34
2 Answers
3
Here is what i have done to save image in sqlite.And i am returning path to the image on succesful image saving, you can change it according to requirement.Here the image is stored in the form of byte array.
Link to my blog post: Cross platform utility to save image in sqlite.
Comman 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();
}
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);
}
}
}

Parth Patel
- 3,937
- 2
- 27
- 44
-1
This question is duplicate of Is it possible to store images in xamarin sqlite
and probably How to store image in SQLite database can help you.

Community
- 1
- 1

Preetika Kaur
- 1,991
- 2
- 16
- 23
-
these all are about only android i want to know about xamarin forms cross platform. Any solution please help – Karan Sep 14 '16 at 05:00
-
Did you try these?? Because db is same SQlite so it will store images in blob only. – Preetika Kaur Sep 14 '16 at 05:01
-
this is in java and they are doing in android activities. I am not getting in xamarin forms where to do it means by dependency i have to do or simply in portable class library. And i have to do it in c# – Karan Sep 14 '16 at 05:05
-
-
-
http://forums.xamarin.com/discussion/22982/saving-image-in-sqlite-database Hey according to discussion on their forums you could go for saving the image on file syatem and store its path in database that will be easy approach and also it ll wrk fine. Do you wanna try this ? – Preetika Kaur Sep 14 '16 at 05:12
-
http://stackoverflow.com/questions/10853301/save-and-load-image-sqlite-c-sharp check this out... – Preetika Kaur Sep 14 '16 at 05:14
-
Thanks for reply, Yeah i already checked this discussion but there is no example how to do ?? – Karan Sep 14 '16 at 05:17
-
That is simple you need to pick the path of file from where you are picking the image and save that path in database. Check this out for your reference: https://developer.xamarin.com/recipes/android/other_ux/pick_image/ – Preetika Kaur Sep 14 '16 at 05:21
-
-
Its my pleasure !! Kindly vote for answer if it helped you and accept it if it works for you. – Preetika Kaur Sep 14 '16 at 05:31