我想以跨平台应用程序的xamarin形式将图像路径存储在sqlite数据库中。如何存储图像路径?
这是我将图像保存在sqlite中所做的工作,并且在成功保存图像时返回了图像的路径,您可以根据需要进行更改。此处图像以字节数组的形式存储。
链接到我的博客文章:跨平台实用程序,用于将图像保存在sqlite中。
通用界面:
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专用代码:
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特定代码:
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);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句