2015-08-21 105 views
3

這是我用來將圖片保存爲我的database.cs中base64String TEXT的代碼。我如何將圖像保存爲Sqlite數據庫爲blob?

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圖像?

+0

有兩種可能的保存方法Image 1.您可以使用位圖將其保存在文件中,然後獲取圖像路徑並將其保存在Db中作爲字符串 –

+0

您可以參考http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it – zzas11

+0

SQLite旨在包含大約400KB的數據。在SQLite Db中存儲圖像不會太有成效。上面寫的圖像路徑方法更好。 –

回答

2

SQLite旨在包含大約400KB的數據。將圖像存儲在SQLite Db中不會很好。上面寫的圖像路徑方法更好。

如果符合您的需求,您最好使用圖像加載庫作爲畢加索,通用圖像加載程序等。這樣你就可以堅持圖片的網址。該圖像由圖庫緩存。

+0

畢加索會自動緩存嗎? – Mogi

1

有保存圖像

兩種可能的方式

1.You可以使用位圖,然後得到圖片的路徑,並在DB存儲作爲字符串

2.Convert成字節數組中的文件保存並將其保存爲Blob

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