Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BitmapTypeAdapter implements JsonSerializer<Bitmap>, JsonDeserializer<Bitmap> {
- @Override
- public Bitmap deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
- byte[] decodedString = Base64.decode(jsonElement.getAsString(), Base64.NO_WRAP);
- return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
- }
- @Override
- public JsonElement serialize(Bitmap bitmap, Type type, JsonSerializationContext jsonSerializationContext) {
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
- byte[] byteArray = stream.toByteArray();
- return new JsonPrimitive(Base64.encodeToString(byteArray, Base64.NO_WRAP));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement