tags:

views:

250

answers:

2

Might sound crazy, but it's what I need to do. I want to take a Bitmap object and use the XMLPullParser/XmlSerializer to write this to a flat file. Obviously I will need to read the XML tag back into a Bitmap object.

I have tried various things, similar to how I write and read Bitmaps from a database.

            Bitmap bitmap = ((BitmapDrawable) icon).getBitmap();                
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 
            bitmap.compress(CompressFormat.PNG, 0, outputStream); 
            byte[] bitmapByte = outputStream.toByteArray(); 

Where I get lost is once I write this out to my XML file as a String, I can never get it converted back properly. Any pointers is appreciated.

Edit: I want to provide a little more information. I am writing out a good deal of XML data for a backup purpose, so loading or writing time is of no concern. This is not a main source of the data (main source is a SQLite database). I do not want to have to write out a couple of very small (48x48pixel) images as well if I can help it.

I am reading in my XML using the XMLPullParser, which reads a String:

if (name.equalsIgnoreCase("someXmlTag")){
String someString = parser.nextText();

I am writing out my XML using the XmlSerializer, which writes a String:

            XmlSerializer serializer = Xml.newSerializer();
        StringWriter writer = new StringWriter();
            serializer.setOutput(writer);
            serializer.startDocument("UTF-8", true);
            serializer.startTag("", "someTag");
                           serializer.text(someString);

So somehow I have to turn my Bitmap into a String and then turn that String back into a Bitmap. I will do some searches on Base64 to see if I get any good examples.

+2  A: 

I would suggest using something like Base64 encoding.

Blorgbeard
+1  A: 

The XMLPullParser is, as its name suggests, a parser, it's not used to write XML. Your best bet is to store it as Base64 like mentioned before. Needless to say, this is going to take a lot of space for no good reason. It will also be a lot slower to read back.

Romain Guy
I have updated my post with some more information that clears up a few things. I will do some searches on Base64 to see what my options are.
pcm2a