@Override public byte[] getBlob(int columnIndex) { return mActiveCursor.getBlob(columnIndex); }
Cursor cur=your query; while(cur.moveToNext()) { byte[] photo=cur.getBlob(index of blob cloumn); }
@Override @Implementation public byte[] getBlob(int i) { return wrappedCursor.getBlob(i); }
public Bitmap getImage(int i){ String qu = "select img from table where feedid=" + i ; Cursor cur = db.rawQuery(qu, null); if (cur.moveToFirst()){ byte[] imgByte = cur.getBlob(0); cur.close(); return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length); } if (cur != null && !cur.isClosed()) { cur.close(); } return null ; }
public static SerializableCookie parseCursorToBean(Cursor cursor) { String host = cursor.getString(cursor.getColumnIndex(HOST)); byte[] cookieBytes = cursor.getBlob(cursor.getColumnIndex(COOKIE)); Cookie cookie = bytesToCookie(cookieBytes); return new SerializableCookie(host, cookie); }
private InputStream getRawAttachmentInputStream(String partId, int location, Cursor cursor) throws FileNotFoundException { switch (location) { case DataLocation.IN_DATABASE: { byte[] data = cursor.getBlob(ATTACH_DATA_INDEX); return new ByteArrayInputStream(data); } case DataLocation.ON_DISK: { File file = getAttachmentFile(partId); return new FileInputStream(file); } default: throw new IllegalStateException("unhandled case"); } }
public static Progress parseCursorToBean(Cursor cursor) { Progress progress = new Progress(); progress.tag = cursor.getString(cursor.getColumnIndex(Progress.TAG)); progress.url = cursor.getString(cursor.getColumnIndex(Progress.URL)); progress.folder = cursor.getString(cursor.getColumnIndex(Progress.FOLDER)); progress.filePath = cursor.getString(cursor.getColumnIndex(Progress.FILE_PATH)); progress.fileName = cursor.getString(cursor.getColumnIndex(Progress.FILE_NAME)); progress.fraction = cursor.getFloat(cursor.getColumnIndex(Progress.FRACTION)); progress.totalSize = cursor.getLong(cursor.getColumnIndex(Progress.TOTAL_SIZE)); progress.currentSize = cursor.getLong(cursor.getColumnIndex(Progress.CURRENT_SIZE)); progress.status = cursor.getInt(cursor.getColumnIndex(Progress.STATUS)); progress.priority = cursor.getInt(cursor.getColumnIndex(Progress.PRIORITY)); progress.date = cursor.getLong(cursor.getColumnIndex(Progress.DATE)); progress.request = (Request<?, ? extends Request>) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(Progress.REQUEST))); progress.extra1 = (Serializable) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(Progress.EXTRA1))); progress.extra2 = (Serializable) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(Progress.EXTRA2))); progress.extra3 = (Serializable) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(Progress.EXTRA3))); return progress; }
public static <T> CacheEntity<T> parseCursorToBean(Cursor cursor) { CacheEntity<T> cacheEntity = new CacheEntity<>(); cacheEntity.setKey(cursor.getString(cursor.getColumnIndex(KEY))); cacheEntity.setLocalExpire(cursor.getLong(cursor.getColumnIndex(LOCAL_EXPIRE))); cacheEntity.setResponseHeaders((HttpHeaders) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(HEAD)))); //noinspection unchecked cacheEntity.setData((T) IOUtils.toObject(cursor.getBlob(cursor.getColumnIndex(DATA)))); return cacheEntity; }
@Override public TestEntity readEntity(Cursor cursor, int offset) { TestEntity entity = new TestEntity( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.getInt(offset + 1), // simpleInt cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2), // simpleInteger cursor.getString(offset + 3), // simpleStringNotNull cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // simpleString cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // indexedString cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // indexedStringAscUnique cursor.isNull(offset + 7) ? null : new java.util.Date(cursor.getLong(offset + 7)), // simpleDate cursor.isNull(offset + 8) ? null : cursor.getShort(offset + 8) != 0, // simpleBoolean cursor.isNull(offset + 9) ? null : cursor.getBlob(offset + 9) // simpleByteArray ); return entity; }
@Override public SimpleEntityNotNull readEntity(Cursor cursor, int offset) { SimpleEntityNotNull entity = new SimpleEntityNotNull( // cursor.getLong(offset + 0), // id cursor.getShort(offset + 1) != 0, // simpleBoolean (byte) cursor.getShort(offset + 2), // simpleByte cursor.getShort(offset + 3), // simpleShort cursor.getInt(offset + 4), // simpleInt cursor.getLong(offset + 5), // simpleLong cursor.getFloat(offset + 6), // simpleFloat cursor.getDouble(offset + 7), // simpleDouble cursor.getString(offset + 8), // simpleString cursor.getBlob(offset + 9) // simpleByteArray ); return entity; }
@Override public void readEntity(Cursor cursor, TestEntity entity, int offset) { entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setSimpleInt(cursor.getInt(offset + 1)); entity.setSimpleInteger(cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2)); entity.setSimpleStringNotNull(cursor.getString(offset + 3)); entity.setSimpleString(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4)); entity.setIndexedString(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5)); entity.setIndexedStringAscUnique(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6)); entity.setSimpleDate(cursor.isNull(offset + 7) ? null : new java.util.Date(cursor.getLong(offset + 7))); entity.setSimpleBoolean(cursor.isNull(offset + 8) ? null : cursor.getShort(offset + 8) != 0); entity.setSimpleByteArray(cursor.isNull(offset + 9) ? null : cursor.getBlob(offset + 9)); }
@Test public void shouldGetBlobFromString() { ContentValues values = new ContentValues(); values.put("first_column", "this is a string"); database.insert("table_name", null, values); Cursor data = database.query("table_name", new String[]{"first_column"}, null, null, null, null, null); assertThat(data.getCount()).isEqualTo(1); data.moveToFirst(); assertThat(data.getBlob(0)).isEqualTo(values.getAsString("first_column").getBytes(UTF_8)); }
@Test public void testGetBlob() throws Exception { String sql = "UPDATE table_name set blob_value=? where id=1234"; byte[] byteData = sql.getBytes(UTF_8); database.execSQL(sql, new Object[]{byteData}); assertThat(cursor.moveToFirst()).isTrue(); byte[] retrievedByteData = cursor.getBlob(5); assertThat(byteData.length).isEqualTo(retrievedByteData.length); for (int i = 0; i < byteData.length; i++) { assertThat(byteData[i]).isEqualTo(retrievedByteData[i]); } }
@Test public void testInsertAndRawQuery() throws Exception { String stringColumnValue = "column_value"; byte[] byteColumnValue = new byte[]{1, 2, 3}; ContentValues values = new ContentValues(); values.put("first_column", stringColumnValue); values.put("second_column", byteColumnValue); database.insert("table_name", null, values); Cursor cursor = database.rawQuery("select second_column, first_column from table_name", null); assertThat(cursor.moveToFirst()).isTrue(); byte[] byteValueFromDatabase = cursor.getBlob(0); String stringValueFromDatabase = cursor.getString(1); assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); }
@Test public void testInsertAndQuery() throws Exception { String stringColumnValue = "column_value"; byte[] byteColumnValue = new byte[]{1, 2, 3}; ContentValues values = new ContentValues(); values.put("first_column", stringColumnValue); values.put("second_column", byteColumnValue); database.insert("table_name", null, values); Cursor cursor = database.query("table_name", new String[]{"second_column", "first_column"}, null, null, null, null, null); assertThat(cursor.moveToFirst()).isTrue(); byte[] byteValueFromDatabase = cursor.getBlob(0); String stringValueFromDatabase = cursor.getString(1); assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); }
@Override public void readEntity(Cursor cursor, SimpleEntityNotNull entity, int offset) { entity.setId(cursor.getLong(offset + 0)); entity.setSimpleBoolean(cursor.getShort(offset + 1) != 0); entity.setSimpleByte((byte) cursor.getShort(offset + 2)); entity.setSimpleShort(cursor.getShort(offset + 3)); entity.setSimpleInt(cursor.getInt(offset + 4)); entity.setSimpleLong(cursor.getLong(offset + 5)); entity.setSimpleFloat(cursor.getFloat(offset + 6)); entity.setSimpleDouble(cursor.getDouble(offset + 7)); entity.setSimpleString(cursor.getString(offset + 8)); entity.setSimpleByteArray(cursor.getBlob(offset + 9)); }
@Test public void testInsertOrThrow() { String stringColumnValue = "column_value"; byte[] byteColumnValue = new byte[]{1, 2, 3}; ContentValues values = new ContentValues(); values.put("first_column", stringColumnValue); values.put("second_column", byteColumnValue); database.insertOrThrow("table_name", null, values); Cursor cursor = database.rawQuery("select second_column, first_column from table_name", null); assertThat(cursor.moveToFirst()).isTrue(); byte[] byteValueFromDatabase = cursor.getBlob(0); String stringValueFromDatabase = cursor.getString(1); assertThat(stringValueFromDatabase).isEqualTo(stringColumnValue); assertThat(byteValueFromDatabase).isEqualTo(byteColumnValue); }
@Override public SimpleEntity readEntity(Cursor cursor, int offset) { SimpleEntity entity = new SimpleEntity( // cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id cursor.isNull(offset + 1) ? null : cursor.getShort(offset + 1) != 0, // simpleBoolean cursor.isNull(offset + 2) ? null : (byte) cursor.getShort(offset + 2), // simpleByte cursor.isNull(offset + 3) ? null : cursor.getShort(offset + 3), // simpleShort cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4), // simpleInt cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5), // simpleLong cursor.isNull(offset + 6) ? null : cursor.getFloat(offset + 6), // simpleFloat cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7), // simpleDouble cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // simpleString cursor.isNull(offset + 9) ? null : cursor.getBlob(offset + 9) // simpleByteArray ); return entity; }
@Test public void shouldCorrectlyReturnNullValues() { database.execSQL("CREATE TABLE null_test (col_int INTEGER, col_text TEXT, col_real REAL, col_blob BLOB)"); ContentValues data = new ContentValues(); data.putNull("col_int"); data.putNull("col_text"); data.putNull("col_real"); data.putNull("col_blob"); assertThat(database.insert("null_test", null, data)).isAtLeast(0L); Cursor nullValuesCursor = database.query("null_test", null, null, null, null, null, null); nullValuesCursor.moveToFirst(); final int colsCount = 4; for (int i = 0; i < colsCount; i++) { assertThat(nullValuesCursor.getType(i)).isEqualTo(Cursor.FIELD_TYPE_NULL); assertThat(nullValuesCursor.getString(i)).isNull(); } assertThat(nullValuesCursor.getBlob(3)).isNull(); }
@Override public void readEntity(Cursor cursor, SimpleEntity entity, int offset) { entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setSimpleBoolean(cursor.isNull(offset + 1) ? null : cursor.getShort(offset + 1) != 0); entity.setSimpleByte(cursor.isNull(offset + 2) ? null : (byte) cursor.getShort(offset + 2)); entity.setSimpleShort(cursor.isNull(offset + 3) ? null : cursor.getShort(offset + 3)); entity.setSimpleInt(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4)); entity.setSimpleLong(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5)); entity.setSimpleFloat(cursor.isNull(offset + 6) ? null : cursor.getFloat(offset + 6)); entity.setSimpleDouble(cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7)); entity.setSimpleString(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8)); entity.setSimpleByteArray(cursor.isNull(offset + 9) ? null : cursor.getBlob(offset + 9)); }