如何在跨平台应用程序的Xamarin Forms中将图像存储在sqlite数据库中?

卡兰

我想以跨平台应用程序的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Xamarin跨平台应用程序中创建具有sqlite支持的PCL

如何在 c# 中获取屏幕尺寸(xamarin 跨平台应用程序)

如何在Android和iOS的Xamarin跨平台应用程序中的App图标上显示徽章计数

无法在 xamarin 中创建跨平台应用程序

如何嵌入罐子以使其在Xamarin跨平台应用程序中可用

如何在跨平台应用程序中处理基于UI的导航?

如何在Xamarin跨平台应用程序中从一个屏幕自动导航到另一个屏幕?

我可以在F#中创建跨平台Xamarin Forms应用程序吗?

我们可以向Xamarin Forms应用程序(跨平台应用程序)添加Apple Watch扩展吗?

我可以使用 MySQL 数据库使用 Xamarin 创建跨平台应用程序吗?

如何以跨平台方式为python存储桌面应用程序数据?

创建从 Visual Studio 中的在线 azure 数据库检索信息的跨平台应用程序

如何在Android中将对象存储到SQLite数据库中?

创建Xamarin.Forms跨平台应用程序时出现“此项目需要Visual Studio更新才能加载”错误

如何使用 Blazor 应用程序在数据库中存储多个图像?

在哪里/如何在数据库中存储有关(SQLite3)数据库的应用程序定义的属性?

如何显示存储在我的 SQLite 数据库中的 BLOB 图像?

保存在数据库中的图像未显示在Xamarin Forms应用程序的Home.xaml页中

SQLite.SQLiteException:xamarin.forms 应用程序中的数据库磁盘映像格式不正确

如何在同一SQL数据库中存储多个应用程序的用户?

带有Xamarin.Forms应用程序的SQLite.net数据库

如何在sqlite数据库(表)中插入图像?

如何在数据库sqlite中添加图像?

如何在应用程序中嵌入数据库?

如何在单个事务中将图像上传到Firebase存储并将引用保存在数据库中?

如何在Xamarin Android中扩展应用程序类

在Xamarin的SQLite数据库中存储和检索数组

通用Windows平台(UWP)C#应用程序-如何在应用程序中运行独立的python

Xamarin 表单:如何在更新应用程序时更新用户的数据库