private Bitmap readFromDb(String name, Bitmap b) { if (mCachedSelectQuery == null) { mCachedSelectQuery = CacheDb.COLUMN_NAME + " = ? AND " + CacheDb.COLUMN_SIZE + " = ?"; } SQLiteDatabase db = mDb.getReadableDatabase(); Cursor result = db.query( CacheDb.TABLE_NAME, new String[] {CacheDb.COLUMN_PREVIEW_BITMAP}, // cols to return mCachedSelectQuery, // select query new String[] {name, mSize}, // args to select query null, null, null, null); if (result.getCount() > 0) { result.moveToFirst(); byte[] blob = result.getBlob(0); result.close(); final BitmapFactory.Options opts = mCachedBitmapFactoryOptions.get(); opts.inBitmap = b; opts.inSampleSize = 1; try { return BitmapFactory.decodeByteArray(blob, 0, blob.length, opts); } catch (IllegalArgumentException e) { removeItemFromDb(mDb, name); return null; } } else { result.close(); return null; } }
private void writeToDb(Object o, Bitmap preview) { String name = getObjectName(o); SQLiteDatabase db = mDb.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(CacheDb.COLUMN_NAME, name); ByteArrayOutputStream stream = new ByteArrayOutputStream(); preview.compress(Bitmap.CompressFormat.PNG, 100, stream); values.put(CacheDb.COLUMN_PREVIEW_BITMAP, stream.toByteArray()); values.put(CacheDb.COLUMN_SIZE, mSize); db.insert(CacheDb.TABLE_NAME, null, values); }