我如何在Sqlite数据库中将图像保存为blob?

时间:2015-08-21 09:19:29

标签: android sqlite blob

这是我用来在我的database.cs中将图片保存为base64String TEXT的代码。

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?

2 个答案:

答案 0 :(得分:2)

SQLite意味着包含大约400KB的数据。在SQLite Db中存储图像不会很好。上面写的图像路径方法更好。

如果符合您的需要,您最好将图像加载库用作Picasso,Universal Image Loader等。这样您就可以保留图像URL。图像由库缓存。

答案 1 :(得分:1)

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

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

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

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