Skip to content

An adapter to serialize Android bitmap files to JSON via Google's GSON library. Will update with better ideas as I come up with them.

Notifications You must be signed in to change notification settings

cnvandev/Android-Bitmap-GSON-Adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

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.

About

An adapter to serialize Android bitmap files to JSON via Google's GSON library. Will update with better ideas as I come up with them.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages