How to write Bitmap to XML file

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

I tried different things, just like I write and read bitmap images from a database.

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

Where I got lost, when I write this to my XML file as a String, I can never convert it correctly. Any pointers are appreciated.

Edit: I want to provide a little more information. I write a lot of XML data for backup purposes, so loading or recording time is not a concern. This is not the main data source (the main source is the SQLite database). I don’t want to write some very small (48x48pixel) images if I can help.

I read in my XML using XMLPullParser which reads a string:

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

I write my XML using an XmlSerializer that 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 need to turn Bitmap into String and then turn this String into Bitmap. I will do some Base64 searches to find out if I have any good examples.

+3
source share
2 answers

I would suggest using something like Base64 .

+2

XMLPullParser, , , XML. Base64, .

, .

.

+1

Source: https://habr.com/ru/post/1729340/


All Articles