微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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?

解决方法:

sqlite旨在包含约400KB的数据.将图像存储在sqlite Db中不会很好.上面写的图像路径方法更好.

如果适合您的需求,最好使用Picasso,Universal Image Loader等图像加载库.这样,您可以只保留图像URL.该图像由库缓存.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐