Advertisement
Ankhwatcher

BitmapTypeAdapter

Feb 4th, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. public class BitmapTypeAdapter implements JsonSerializer<Bitmap>, JsonDeserializer<Bitmap> {
  2.  
  3.  
  4.     @Override
  5.     public Bitmap deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
  6.  
  7.         byte[] decodedString = Base64.decode(jsonElement.getAsString(), Base64.NO_WRAP);
  8.         return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
  9.     }
  10.  
  11.     @Override
  12.     public JsonElement serialize(Bitmap bitmap, Type type, JsonSerializationContext jsonSerializationContext) {
  13.         ByteArrayOutputStream stream = new ByteArrayOutputStream();
  14.         bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
  15.         byte[] byteArray = stream.toByteArray();
  16.         return new JsonPrimitive(Base64.encodeToString(byteArray, Base64.NO_WRAP));
  17.     }
  18.  
  19.  
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement