如何将图像保存为blob格式到SQLite数据库中?

3
这是我在database.cs中使用的代码,用于将图片保存为base64String文本格式的内容。
public void CreateDatabase(string sqldb_name)
        {
            try
            {
                sqldb_message = "";
                string sqldb_location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                string sqldb_path = Path.Combine(sqldb_location, sqldb_name);
                bool sqldb_exists = File.Exists(sqldb_path);
                if(!sqldb_exists)
                {

                    //Adding new fields
                    //For adding new fields we have to define the field on the "create" query, in this case, we're adding "BirthDay" field which is VARCHAR
                    sqldb = SQLiteDatabase.OpenOrCreateDatabase(sqldb_path,null);
                    sqldb_query = "CREATE TABLE IF NOT EXISTS MyTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, Art TEXT, Album VARCHAR, Artist VARCHAR, Country VARCHAR, Year INT, Genre VARCHAR, Style VARCHAR, Format VARCHAR);";

                    sqldb.ExecSQL(sqldb_query);
                    sqldb_message = "Database: " + sqldb_name + " created";
                }
                else
                {
                    sqldb = SQLiteDatabase.OpenDatabase(sqldb_path, null, DatabaseOpenFlags.OpenReadwrite);
                    sqldb_message = "Database: " + sqldb_name + " opened";
                }
                sqldb_available=true;
            }
            catch(SQLiteException ex) 
            {
                sqldb_message = ex.Message;
            }
        }
        //Adds a new record with the given parameters

        //Adding new fields
        //We have to modify the CRUD (Create, Read, Update and Delete) operations by adding the new "BirthDay" field, in this case, we're adding the new string parameter "sBirthDay"
        public void AddRecord(string @IMG, string sAlbum, string sArtist, string sCountry, int iYear, string sGenre, string sStyle, string sFormat)
        {
            try
            {
                //Adding new fields
                //We have to modify the CRUD (Create, Read, Update and Delete) operations by adding the new "BirthDay" field to the query and assign the value "sBirthDay"
                sqldb_query = "INSERT INTO MyTable (Art, Album, Artist, Country, Year, Genre, Style, Format) VALUES ('" + @IMG + "','" + sAlbum + "','" + sArtist + "','" + sCountry + "','" + iYear + "','" + sGenre + "','" + sStyle + "','" + sFormat + "');";

                sqldb.ExecSQL(sqldb_query);
                sqldb_message = "Record saved";
            }
            catch(SQLiteException ex) 
            {
                sqldb_message = ex.Message;
            }
        }

我应该对代码做哪些修改才能将图像保存为数据库中的blob?


有两种可能的方法可以保存图像:
  1. 您可以使用Bitmap将其保存在文件中,然后获取图像路径,并将其保存为字符串存储在数据库中。
- Rajan Kali
你可以参考这个链接:https://dev59.com/gWs05IYBdhLWcg3wSP9H - zzas11
SQLite旨在包含约400KB的数据。将图像存储在SQLite数据库中并不会太有用。上面写的图像路径方法更好。 - Niraj Adhikari
2个回答

2

保存图像有两种可能的方法

1.您可以使用Bitmap将其保存在文件中,然后获取图像路径并将其保存为字符串

2.将其转换为字节数组并保存为Blob

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

2

SQLite的用途是存储大约 ~400KB 的数据。将图像存储在SQLite数据库中并不好。上面写的图像路径方法更好。

如果符合您的需求,最好使用像Picasso、Universal Image Loader等图像加载库。这样,您只需持久化图像URL。该库会缓存图像。


Picasso会自动缓存吗? - mohsen_og

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接