数据库 存图片
来源:互联网 发布:炒股分析软件 编辑:程序博客网 时间:2024/05/08 23:13
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);}