private static void createBitmTable(SQLiteDatabase db) {
String bitmapTable = "create table if not exists bitmap (data BLOB)";
db.execSQL(bitmapTable);
}
public static void insertBitmap(SQLiteDatabase db, byte[] bytes) {
String sqlString = "insert into bitmap values (?)";
Object[] valueObjects = new Object[] {bytes};
db.execSQL(sqlString, valueObjects);
}
public static byte[] selectBitmap(SQLiteDatabase db) {
String sqlString = "select * from bitmap";
byte[] bitmap = null;
Cursor cursor = null;
cursor = db.rawQuery(sqlString,null);
if (cursor.moveToNext()) {
bitmap = cursor.getBlob(cursor.getColumnIndex("data"));
}
cursor.close();
return bitmap;
}
private void showBitmap() {
// 获取指定图片的二进制
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.ic_launcher)).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); // 压缩为PNG格式,100表示跟原图大小一样
byte[] source = outputStream.toByteArray();
// 存
DataBaseHelp.insertBitmap(mDb, source);
// 取
byte[] bimapFromDb = DataBaseHelp.selectBitmap(mDb);
// 展现
Bitmap bitmap2 = BitmapFactory.decodeByteArray(bimapFromDb, 0, bimapFromDb.length);
mImageView.setImageBitmap(bitmap2);
}
数据库 存图片
最新推荐文章于 2023-01-01 12:45:50 发布