BOLB stands for binary large object, which is a data type used to store pictures, images, videos, etc.
First, the use of scenarios:
/s/blog _ 8 CFBB 9920 10 12 oqn . html
The public class MySQLiteOpenHelper extends SQLiteOpenHelper {
1, override the constructor
Public MySQLiteOpenHelper (Context, string name,
CursorFactory cursor, int version) (
Super (context, name, cursor, version);
}
2, the method of creating a database
public void onCreate(SQLite database db){
3. Create a database with table name: imagetable and fields: _id, image.
db . exec SQL(" CREATE TABLE image TABLE(_ id INTEGER PRIMARY KEY auto increment,image BLOB)");
}
4, the method of updating the database
public void on upgrade(SQLite database db,int oldVersion,int newVersion) {
}
}
5. Create an instance of the helper class.
The value of CursorFactory is null, which means the default factory class is adopted.
mySQLiteOpenHelper = new mySQLiteOpenHelper(this," saveimage.db ",null, 1);
6. Create a readable database.
mydb = mysqliteopenhelper . getwritabledatabase();
7. Convert the picture into a bitmap.
bitmap bitmap 1 = bitmapfactory . decode resource(get resources(),r . drawable . erweima);
int size = bitmap 1 . getwidth()* bitmap 1 . getheight()* 4;
//Create a byte array output stream with size.
ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
//Set the bitmap compression format with the quality of 100%, and put it into the output stream of byte array bitmap1.compress (bitmap.compresseformat.png, 100, baos);
//Convert byte array output stream into byte array byte[]
byte[]image data 1 = baos . tobytearray();
//Save the byte array to the database.
content values cv = new content values();
cv.put("_id ", 1);
cv.put("image ",imagedata 1);
mydb.insert("imagetable ",null,cv);
//Close the byte array output stream
baos . close();
Second, the query method from the database:
1, create a pointer.
cursor cur = mydb . query(" imagetable ",new String[]{"_id "," image"},null,null,null,null,null);
byte[]image query = null;
if(cur.moveToNext()){
2. convert Blob data into byte array imagequery = cur.getblob (cur.getcolumnindex ("image"));
}
3. Convert byte array into bitmap.
bitmap image bitmap = bitmap factory . decodebytearray(imagequery,0,image query . length);
iv 1 =(ImageView)findViewById(r . id . ImageView 1);
4. Display the bitmap as a picture.
iv 1 . setimagebitmap(image bitmap);