开发者

How to write a Bitmap out to a XML file

开发者 https://www.devze.com 2022-12-17 10:01 出处:网络
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

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 XM开发者_JAVA技巧L 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.


I would suggest using something like Base64 encoding.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消