每日总结

1.今天学习sqlite的二进制文件的存取

①在创建数据库表的时候,需要创建一个BLOB的宇段,用于存储二进制的值

db.execSQL("Create table test (_id INTEGER PRIMARY KEY AUTOINCREMENT,head_img BLOB));


②将图片转换为BLOB格式(这里是以ImageView为例的,如果是普通图片只需要转换成Bitmap再调用即可)
SQLiteDatabase db = mDBService.getWritableDatabase0;得到数据库
try {
ByteArrayOutputStream outs = new ByteArrayOutputStream0;
((BitmapDrawable) imageview.getDrawable0).getBitmap0.compress(
CompressFormat.PNG, 100,outs);//压缩为PNG格式,100表示跟原图大小一样Objectl args = new Object{outs.toByteArray0 }
db.execsQL("INSERT INTO test(head_img) values(7)", args);outs.close0;
db.close0;
}catch (Exception e) {e.printStackTrace0;}
2.读取SQLite中的图片:
sQLiteDatabase db = mDBService.getreadableDatabase0;
Cursor cursor = db.rawQuery("SELECT head_img FROM test",null);if(cursor != null)
if(cursor.moveToFirst0){
1/取出图片保存到宇节数组中
byte img = cursor.getBlob(cursor.getColumnIndex("head_img");
if(cursor != null)cursor.close0;
J/将图片显示到ImageView上if(img != nulD
ByteArrayInputStream bin = new ByteArrayInputStream(img);

原文地址:https://www.cnblogs.com/chenghaixiang/p/14908557.html