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:za.co.neilson.alarm.database.Database.java

public static List<Alarm> getAll() {
    List<Alarm> alarms = new ArrayList<Alarm>();
    Cursor cursor = Database.getCursor();
    if (cursor.moveToFirst()) {

        do {//  ww  w.j  a  v a2s. c  om
            // COLUMN_ALARM_ID,
            // COLUMN_ALARM_ACTIVE,
            // COLUMN_ALARM_TIME,
            // COLUMN_ALARM_DAYS,
            // COLUMN_ALARM_DIFFICULTY,
            // COLUMN_ALARM_TONE,
            // COLUMN_ALARM_VIBRATE,
            // COLUMN_ALARM_NAME

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

            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()[cursor.getInt(4)]);
            alarm.setAlarmTonePath(cursor.getString(5));
            alarm.setVibrate(cursor.getInt(6) == 1);
            alarm.setAlarmName(cursor.getString(7));

            alarms.add(alarm);

        } while (cursor.moveToNext());
    }
    cursor.close();
    return alarms;
}

From source file:li.barter.utils.Utils.java

/**
 * Converts a cursor to a bundle. Field datatypes will be maintained. Floats will be stored in
 * the Bundle as Doubles, and Integers will be stored as Longs due to Cursor limitationcs
 *
 * @param cursor The cursor to convert to a Bundle. This must be positioned to the row to be
 *               read/* www.ja  v a2s.c o m*/
 * @return The converted bundle
 */
public static Bundle cursorToBundle(Cursor cursor) {

    final int columnCount = cursor.getColumnCount();
    final Bundle bundle = new Bundle(columnCount);

    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {

        final String columnName = cursor.getColumnName(columnIndex);
        switch (cursor.getType(columnIndex)) {

        case Cursor.FIELD_TYPE_STRING: {
            bundle.putString(columnName, cursor.getString(columnIndex));
            break;
        }

        case Cursor.FIELD_TYPE_BLOB: {
            bundle.putByteArray(columnName, cursor.getBlob(columnIndex));
            break;
        }

        case Cursor.FIELD_TYPE_FLOAT: {
            bundle.putDouble(columnName, cursor.getDouble(columnIndex));
            break;
        }

        case Cursor.FIELD_TYPE_INTEGER: {
            bundle.putLong(columnName, cursor.getLong(columnIndex));
            break;
        }
        }
    }

    return bundle;
}

From source file:org.droidparts.inner.converter.ModelConverter.java

@Override
public <G1, G2> M readFromCursor(Class<M> valType, Class<G1> genericArg1, Class<G2> genericArg2, Cursor cursor,
        int columnIndex) throws Exception {
    M model = null;//from w ww  .  ja  va  2  s.c o  m
    byte[] arr = cursor.getBlob(columnIndex);
    if (arr != null) {
        model = (M) PersistUtils.fromBytes(arr);
    }
    return model;
}

From source file:at.flack.MainActivity.java

public static final Bitmap fetchThumbnail(Activity context, String number) {
    Integer id = fetchThumbnailId(context, number);
    if (id == null)
        return null;
    final Uri uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id);
    final Cursor cursor = context.getContentResolver().query(uri, SMSItem.PHOTO_BITMAP_PROJECTION, null, null,
            null);/*from  ww w .jav a 2 s  .co m*/

    try {
        Bitmap thumbnail = null;
        if (cursor.moveToFirst()) {
            final byte[] thumbnailBytes = cursor.getBlob(0);
            if (thumbnailBytes != null) {
                thumbnail = BitmapFactory.decodeByteArray(thumbnailBytes, 0, thumbnailBytes.length);
            }
        }
        return thumbnail;
    } finally {
        cursor.close();
    }
}

From source file:com.taobao.weex.devtools.inspector.protocol.module.Database.java

/**
 * Flatten all columns and all rows of a cursor to a single array.  The array cannot be
 * interpreted meaningfully without the number of columns.
 *
 * @param cursor//from   ww w . j  a  v  a 2  s. co  m
 * @param limit Maximum number of rows to process.
 * @return List of Java primitives matching the value type of each column, converted to
 *      strings.
 */
private static ArrayList<String> flattenRows(Cursor cursor, int limit) {
    Util.throwIfNot(limit >= 0);
    ArrayList<String> flatList = new ArrayList<>();
    final int numColumns = cursor.getColumnCount();
    for (int row = 0; row < limit && cursor.moveToNext(); row++) {
        for (int column = 0; column < numColumns; column++) {
            switch (cursor.getType(column)) {
            case Cursor.FIELD_TYPE_NULL:
                flatList.add(null);
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                flatList.add(String.valueOf(cursor.getLong(column)));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                flatList.add(String.valueOf(cursor.getDouble(column)));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                flatList.add(blobToString(cursor.getBlob(column)));
                break;
            case Cursor.FIELD_TYPE_STRING:
            default:
                flatList.add(cursor.getString(column));
                break;
            }
        }
    }
    if (!cursor.isAfterLast()) {
        for (int column = 0; column < numColumns; column++) {
            flatList.add("{truncated}");
        }
    }
    return flatList;
}

From source file:com.luorrak.ouroboros.deepzoom.DeepZoomActivity.java

public void newMediaListInstance(InfiniteDbHelper infiniteDbHelper, String resto) {
    if (mediaList == null) {
        mediaList = new ArrayList<Media>();
        Cursor cursor = infiniteDbHelper.getThreadCursor(resto);
        do {/*from w  ww . j av  a 2  s  .c  o  m*/
            byte[] serializedPostMedia = cursor
                    .getBlob(cursor.getColumnIndex(DbContract.ThreadEntry.COLUMN_THREAD_MEDIA_FILES));
            if (serializedPostMedia != null) {
                mediaList.addAll((Collection<? extends Media>) Util.deserializeObject(serializedPostMedia));
            }
        } while (cursor.moveToNext());
        cursor.close();
    }
}

From source file:com.github.shareme.gwschips.library.BaseRecipientAdapter.java

protected static void fetchPhoto(final RecipientEntry entry, final Uri photoThumbnailUri,
        final ContentResolver mContentResolver) {
    byte[] photoBytes = mPhotoCacheMap.get(photoThumbnailUri);
    if (photoBytes != null) {
        entry.setPhotoBytes(photoBytes);
        return;//from ww  w  . j a v  a2 s  .  c om
    }
    final Cursor photoCursor = mContentResolver.query(photoThumbnailUri, PhotoQuery.PROJECTION, null, null,
            null);
    if (photoCursor != null) {
        try {
            if (photoCursor.moveToFirst()) {
                photoBytes = photoCursor.getBlob(PhotoQuery.PHOTO);
                entry.setPhotoBytes(photoBytes);
                mPhotoCacheMap.put(photoThumbnailUri, photoBytes);
            }
        } finally {
            photoCursor.close();
        }
    } else {
        InputStream inputStream = null;
        ByteArrayOutputStream outputStream = null;
        try {
            inputStream = mContentResolver.openInputStream(photoThumbnailUri);
            final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            if (bitmap != null) {
                outputStream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                photoBytes = outputStream.toByteArray();

                entry.setPhotoBytes(photoBytes);
                mPhotoCacheMap.put(photoThumbnailUri, photoBytes);
            }
        } catch (final FileNotFoundException e) {
            Log.w(TAG, "Error opening InputStream for photo", e);
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                Log.e(TAG, "Error closing photo input stream", e);
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                Log.e(TAG, "Error closing photo output stream", e);
            }
        }
    }
}

From source file:com.luorrak.ouroboros.gallery.GalleryFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    setHasOptionsMenu(true);/*from  ww w.ja va 2 s.  co  m*/
    infiniteDbHelper = new InfiniteDbHelper(getActivity());
    networkHelper = new NetworkHelper();
    ArrayList<Media> mediaArrayList = new ArrayList<>();

    View view = inflater.inflate(R.layout.fragment_gallery, container, false);
    getActivity().setTitle("Gallery");
    if (getArguments() != null) {
        boardName = getArguments().getString("boardName");
        resto = getArguments().getString("resto");
    }

    Cursor cursor = infiniteDbHelper.getThreadCursor(resto);
    do {
        byte[] serializedPostMedia = cursor
                .getBlob(cursor.getColumnIndex(DbContract.ThreadEntry.COLUMN_THREAD_MEDIA_FILES));
        if (serializedPostMedia != null) {
            mediaArrayList.addAll((Collection<? extends Media>) Util.deserializeObject(serializedPostMedia));
        }
    } while (cursor.moveToNext());

    cursor.close();

    recyclerView = (RecyclerView) view.findViewById(R.id.gallery_list);
    gridLayoutManager = new GridLayoutManager(getActivity(), 3);
    recyclerView.setLayoutManager(gridLayoutManager);
    galleryAdapter = new GalleryAdapter(mediaArrayList, boardName, resto, getActivity());
    recyclerView.setAdapter(galleryAdapter);

    return view;
}

From source file:com.github.shareme.gwschips.library.BaseRecipientAdapter.java

private static void fetchPhotoAsync(final RecipientEntry entry, final Uri photoThumbnailUri,
        final BaseAdapter adapter, final ContentResolver mContentResolver) {
    final AsyncTask<Void, Void, byte[]> photoLoadTask = new AsyncTask<Void, Void, byte[]>() {
        @Override//  ww  w  .  j  a  v  a 2s .  c  o  m
        protected byte[] doInBackground(Void... params) {
            // First try running a query. Images for local contacts are
            // loaded by sending a query to the ContactsProvider.
            final Cursor photoCursor = mContentResolver.query(photoThumbnailUri, PhotoQuery.PROJECTION, null,
                    null, null);
            if (photoCursor != null) {
                try {
                    if (photoCursor.moveToFirst()) {
                        return photoCursor.getBlob(PhotoQuery.PHOTO);
                    }
                } finally {
                    photoCursor.close();
                }
            } else {
                // If the query fails, try streaming the URI directly.
                // For remote directory images, this URI resolves to the
                // directory provider and the images are loaded by sending
                // an openFile call to the provider.
                try {
                    InputStream is = mContentResolver.openInputStream(photoThumbnailUri);
                    if (is != null) {
                        byte[] buffer = new byte[BUFFER_SIZE];
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        try {
                            int size;
                            while ((size = is.read(buffer)) != -1) {
                                baos.write(buffer, 0, size);
                            }
                        } finally {
                            is.close();
                        }
                        return baos.toByteArray();
                    }
                } catch (IOException ex) {
                    // ignore
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(final byte[] photoBytes) {
            entry.setPhotoBytes(photoBytes);
            if (photoBytes != null) {
                mPhotoCacheMap.put(photoThumbnailUri, photoBytes);
                if (adapter != null)
                    adapter.notifyDataSetChanged();
            }
        }
    };
    photoLoadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}

From source file:com.luorrak.ouroboros.gallery.GalleryFragment.java

public void startDownload() {
    Cursor imageCursor = infiniteDbHelper.getGalleryCursor(resto);
    do {/*w  w  w  .  jav a  2 s . co  m*/
        ArrayList<Media> mediaArrayList = (ArrayList<Media>) Util.deserializeObject(imageCursor
                .getBlob(imageCursor.getColumnIndex(DbContract.ThreadEntry.COLUMN_THREAD_MEDIA_FILES)));
        for (Media mediaItem : mediaArrayList) {
            networkHelper.downloadFile(boardName, mediaItem.fileName, mediaItem.ext, getActivity());
        }
    } while (imageCursor.moveToNext());

    imageCursor.close();
}