Code Monkey home page Code Monkey logo

android-bitmap-gson-adapter's Introduction

Android Bitmap GSON Adapter

This is a GSON adapter I came up with to serialize and deserialize Android bitmap drawable files to GSON. Right now it's a little clunky - you have to wrap your Bitmaps in a NamedBitmap which holds the name of the file - and it needs to be the same as the filename sans extension (as in what you'd use to request it from Android by name) - but I'll come up with a better idea. For now, it works.

How To Use

The first thing to do is to adapt your code to use NamedBitmaps, and to set those names to equal the filenames. I know, I know, it sucks, but the Bitmap .equals() method below Gingerbread (or so) uses the memory address, which makes it difficult to compare two bitmaps that have been loaded seperately. To load the actual Bitmap from a NamedBitmap, just call getBitmap() on it - it's not too difficult.

To use the Adapter in your GSON conversion process, you need to register it as a Type Adapter like so:

NamedBitmapDeserializer bitmapAdapter = new NamedBitmapDeserializer(context);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(NamedBitmap.class, bitmapAdapter);
Gson gson = gsonBuilder.create();

Then, you just serialize it like this

gson.toJson(yourObject);

and deserialize it like this

gson.fromJson(reader, YourObject.class);

Ta da! Suggestions on how to make this not suck (besides "Use sameAs(Bitmap)!" Yeah buddy, I get it) are more than welcome.

android-bitmap-gson-adapter's People

Contributors

cnvandev avatar

Watchers

James Cloos avatar Jeffrey Zhao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.