Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. // save to db
  2. private void saveItem() {
  3. ContentValues values = new ContentValues();
  4. values.put(InventoryEntry.COLUMN_IMAGE, BitmapUtility.bitmapToBlob(mBitmap));
  5. }
  6.  
  7. // retrieve from db (via CursorLoader)
  8. public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
  9. int imageIndex = cursor.getColumnIndex(InventoryEntry.COLUMN_IMAGE);
  10. byte[] imageBlob = cursor.getBlob(imageIndex);
  11.  
  12. if (imageBlob != null && imageBlob.length > 0 && !flagBitmapSet) {
  13. mBitmap = BitmapUtility.blobToBitmap(imageBlob);
  14. //TODO: display image
  15. }
  16. }
  17.  
  18. // convert from bitmap to byte array
  19. private byte[] bitmapToBlob(Bitmap bitmap) {
  20. byte[] byteStream = new byte[0];
  21. try {
  22. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  23. bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream);
  24. byteStream = stream.toByteArray();
  25. stream.close();
  26. } catch (NullPointerException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. } finally {
  31. return byteStream;
  32. }
  33. }
  34.  
  35. // convert from byte array to bitmap
  36. private Bitmap blobToBitmap(byte[] bytes) {
  37. return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement