Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // save to db
- private void saveItem() {
- ContentValues values = new ContentValues();
- values.put(InventoryEntry.COLUMN_IMAGE, BitmapUtility.bitmapToBlob(mBitmap));
- }
- // retrieve from db (via CursorLoader)
- public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
- int imageIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_IMAGE);
- byte[] imageBlob = cursor.getBlob(imageIndex);
- if (imageBlob != null && imageBlob.length > 0 && !flagBitmapSet) {
- mBitmap = BitmapUtility.blobToBitmap(imageBlob);
- //TODO: display image
- }
- }
- // convert from bitmap to byte array
- private byte[] bitmapToBlob(Bitmap bitmap) {
- byte[] byteStream = new byte[0];
- try {
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
- byteStream = stream.toByteArray();
- stream.close();
- } catch (NullPointerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- return byteStream;
- }
- }
- // convert from byte array to bitmap
- private Bitmap blobToBitmap(byte[] bytes) {
- return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement