Example usage for android.database Cursor getBlob

List of usage examples for android.database Cursor getBlob

Introduction

In this page you can find the example usage for android.database Cursor getBlob.

Prototype

byte[] getBlob(int columnIndex);

Source Link

Document

Returns the value of the requested column as a byte array.

Usage

From source file:org.awesomeapp.messenger.ui.legacy.DatabaseUtils.java

public static byte[] getAvatarBytesFromAddress(ContentResolver cr, String address, int width, int height)
        throws DecoderException {

    String[] projection = { Imps.Avatars.DATA };
    String[] args = { address };/*from  ww  w  .  j a  v  a2  s .  com*/
    String query = Imps.Avatars.CONTACT + " LIKE ?";
    Cursor cursor = cr.query(Imps.Avatars.CONTENT_URI, projection, query, args,
            Imps.Avatars.DEFAULT_SORT_ORDER);

    byte[] data = null;

    if (cursor != null) {
        if (cursor.moveToFirst())
            data = cursor.getBlob(0);

        cursor.close();
    }

    return data;

}

From source file:org.tigase.mobile.utils.AvatarHelper.java

protected static Bitmap loadAvatar(BareJID jid, int size) {
    Bitmap bmp = null;/*from   w ww  .j  a  v a 2 s  . c o m*/

    Log.v(TAG, "loading avatar with size " + size);

    Cursor cursor = context.getContentResolver().query(
            Uri.parse(RosterProvider.VCARD_URI + "/" + Uri.encode(jid.toString())), null, null, null, null);
    try {
        if (cursor.moveToNext()) {
            // we found avatar in our store
            byte[] avatar = cursor.getBlob(cursor.getColumnIndex(VCardsCacheTableMetaData.FIELD_DATA));
            if (avatar != null) {
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                Bitmap bmp1 = BitmapFactory.decodeByteArray(avatar, 0, avatar.length, options);
                if (bmp1 != null) {
                    bmp1.recycle();
                }
                // options.inSampleSize = calculateSize(options, 96, 96);
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                options.inSampleSize = calculateSize(options, size, size);
                options.inJustDecodeBounds = false;
                bmp = BitmapFactory.decodeByteArray(avatar, 0, avatar.length, options);
            }
        } else {
            // no avatar in our store - checking for avatar in Android
            // contacts
            Uri photoUri = SyncAdapter.getAvatarUriFromContacts(context.getContentResolver(), jid);
            if (photoUri != null) {
                InputStream input = ContactsContract.Contacts
                        .openContactPhotoInputStream(context.getContentResolver(), photoUri);
                if (input != null) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    Bitmap bmp1 = BitmapFactory.decodeStream(input, null, options);
                    if (bmp1 != null) {
                        bmp1.recycle();
                    }
                    // options.inSampleSize = calculateSize(options, 96,
                    // 96);
                    input.close();
                    input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
                            photoUri);
                    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                    options.inSampleSize = calculateSize(options, size, size);
                    options.inJustDecodeBounds = false;
                    bmp = BitmapFactory.decodeStream(input, null, options);
                    input.close();
                }
            }
        }
    } catch (Exception ex) {
        Log.v(TAG, "exception retrieving avatar for " + jid.toString(), ex);
    } finally {
        cursor.close();
    }
    return bmp;
}

From source file:Main.java

public static Object cursorValue(String column, Cursor cr) {
    Object value = false;//from   w  w  w .  java2 s  . com
    int index = cr.getColumnIndex(column);
    switch (cr.getType(index)) {
    case Cursor.FIELD_TYPE_NULL:
        value = false;
        break;
    case Cursor.FIELD_TYPE_STRING:
        value = cr.getString(index);
        break;
    case Cursor.FIELD_TYPE_INTEGER:
        value = cr.getInt(index);
        break;
    case Cursor.FIELD_TYPE_FLOAT:
        value = cr.getFloat(index);
        break;
    case Cursor.FIELD_TYPE_BLOB:
        value = cr.getBlob(index);
        break;
    }
    return value;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static Bitmap openPhoto(Context context, long contactId) {

    Log.e(TAG, "openPhoto");
    if (context == null) {
        return null;
    }// www  .j  a  v a 2s.c om

    Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
    Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { ContactsContract.Contacts.Photo.PHOTO }, null, null, null);
    if (cursor == null) {
        return null;
    }

    try {
        if (cursor.moveToFirst()) {
            byte[] data = cursor.getBlob(0);
            if (data != null) {
                return BitmapFactory.decodeStream(new ByteArrayInputStream(data));
            }
        }
    } finally {
        cursor.close();
    }
    return null;
}

From source file:com.android.xbrowser.BookmarksPageCallbacks.java

static Bitmap getBitmap(Cursor cursor, int columnIndex) {
    byte[] data = cursor.getBlob(columnIndex);
    if (data == null) {
        return null;
    }/*ww w . ja  v a2 s.c o m*/
    return BitmapFactory.decodeByteArray(data, 0, data.length);
}

From source file:com.android.browser.BookmarksPageCallbacks.java

static Bitmap getBitmap(Cursor cursor, int columnIndex, Bitmap inBitmap) {
    byte[] data = cursor.getBlob(columnIndex);
    if (data == null) {
        return null;
    }/* www. j a  va  2 s. co  m*/
    Options opts = sOptions.get();
    opts.inBitmap = inBitmap;
    opts.inSampleSize = 1;
    opts.inScaled = false;
    try {
        return BitmapFactory.decodeByteArray(data, 0, data.length, opts);
    } catch (IllegalArgumentException ex) {
        // Failed to re-use bitmap, create a new one
        return BitmapFactory.decodeByteArray(data, 0, data.length);
    }
}

From source file:com.contentful.vault.SqliteHelper.java

@SuppressWarnings("unchecked")
private static Asset assetFromCursor(Cursor cursor) {
    String remoteId = cursor.getString(resourceColumnIndex(REMOTE_ID));
    String url = cursor.getString(assetColumnIndex(Asset.Fields.URL));
    String mimeType = cursor.getString(assetColumnIndex(Asset.Fields.MIME_TYPE));
    String title = cursor.getString(assetColumnIndex(Asset.Fields.TITLE));
    String description = cursor.getString(assetColumnIndex(Asset.Fields.DESCRIPTION));
    HashMap<String, Object> fileMap = null;
    byte[] fileBlob = cursor.getBlob(assetColumnIndex(Asset.Fields.FILE));

    if (fileBlob != null && fileBlob.length > 0) {
        try {/*  www .  j av  a2 s .  c o  m*/
            fileMap = BlobUtils.fromBlob(HashMap.class, fileBlob);
        } catch (IOException e) {
            throw new RuntimeException("Failed while deserializing file map for asset '" + remoteId + "'.");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    return Asset.builder().setUrl(url).setMimeType(mimeType).setTitle(title).setDescription(description)
            .setFile(fileMap).build();
}

From source file:za.co.neilson.alarm.database.Database.java

public static Alarm getAlarm(int id) {
    // TODO Auto-generated method stub
    String[] columns = new String[] { COLUMN_ALARM_ID, COLUMN_ALARM_ACTIVE, COLUMN_ALARM_TIME,
            COLUMN_ALARM_DAYS, COLUMN_ALARM_DIFFICULTY, COLUMN_ALARM_TONE, COLUMN_ALARM_VIBRATE,
            COLUMN_ALARM_NAME };/*www .ja  v  a2s  . com*/
    Cursor c = getDatabase().query(ALARM_TABLE, columns, COLUMN_ALARM_ID + "=" + id, null, null, null, null);
    Alarm alarm = null;

    if (c.moveToFirst()) {

        alarm = new Alarm();
        alarm.setId(c.getInt(1));
        alarm.setAlarmActive(c.getInt(2) == 1);
        alarm.setAlarmTime(c.getString(3));
        byte[] repeatDaysBytes = c.getBlob(4);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(repeatDaysBytes);
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Alarm.Day[] repeatDays;
            Object object = objectInputStream.readObject();
            if (object instanceof Alarm.Day[]) {
                repeatDays = (Alarm.Day[]) object;
                alarm.setDays(repeatDays);
            }
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        alarm.setDifficulty(Difficulty.values()[c.getInt(5)]);
        alarm.setAlarmTonePath(c.getString(6));
        alarm.setVibrate(c.getInt(7) == 1);
        alarm.setAlarmName(c.getString(8));
    }
    c.close();
    return alarm;
}

From source file:Main.java

private static String dumpRow(Cursor c) {
    String result = "VALUES(";

    for (int pos = 0; pos < c.getColumnCount(); pos++) {
        switch (c.getType(pos)) {
        case Cursor.FIELD_TYPE_NULL:
            result += "null";
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            result += Integer.toString(c.getInt(pos));
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            result += Float.toString(c.getFloat(pos));
            break;
        case Cursor.FIELD_TYPE_STRING:
            result += DatabaseUtils.sqlEscapeString(c.getString(pos));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            result += "X'";
            for (byte b : c.getBlob(pos)) {
                result += String.format("%02X", b);
            }//from  ww w . j a  v  a 2  s  .co m
            result += "'";
            break;
        default:
            return "Invalid field type " + c.getType(pos) + " at position " + pos;
        }

        if (pos < c.getColumnCount() - 1) {
            result += ", ";
        }
    }
    return result + ")";
}

From source file:Main.java

public static String logCursor(String prefix, Cursor cr) {
    StringBuilder sb = new StringBuilder().append(prefix + ": ");
    for (int i = 0; i < cr.getColumnCount(); i++) {
        sb.append(cr.getColumnName(i)).append("=");
        switch (cr.getType(i)) {
        case Cursor.FIELD_TYPE_NULL:
            sb.append("NULL");
            break;
        case Cursor.FIELD_TYPE_STRING:
            sb.append("\"").append(cr.getString(i)).append("\"");
            break;
        case Cursor.FIELD_TYPE_INTEGER:
            sb.append(cr.getInt(i));/*from  ww  w . j  ava 2 s  .c  o  m*/
            break;
        case Cursor.FIELD_TYPE_FLOAT:
            sb.append(cr.getFloat(i));
            break;
        case Cursor.FIELD_TYPE_BLOB:
            sb.append("BIN(").append(cr.getBlob(i).length).append("b)");
            break;
        }
        sb.append(" ");
    }
    return sb.toString();
}