code: public static byte[] toByteArray(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
          ByteArrayOutputStream out = null;
          try {
              out = new ByteArrayOutputStream();
              bitmap.compress(format, quality, out);
              return out.toByteArray();
          } finally {
              CloseableUtils.close(out);
          }
      }
docs: Compress the bitmap to the byte array as the specified format and quality.
  @param bitmap to compress.
  @param format the format.
  @param quality the quality of the compressed bitmap.
  @return the compressed bitmap byte array.