ByteArray To XML File On Android

I am sending a byte [] file from a web service to an Android device.

  • If this file is an XML file, that is byte [], then how can I convert it to the original XML file.
  • If this file is an image array, that is, byte [], how can I convert it to the original image.

I am using android sdk 2.2 on the galaxy galaxy tab.

+3
source share
2 answers

Your web service should send you some identifier about the file type. whether an array of bytes for an image or for a shared file. then only you can find out what type of file it is. after knowing the file type, you can convert the byte array to the desired file type. You can also write to a file. If you want to print this xml in logcat, you can use

String xmlData = new String(byte[] data); System.out.println(xmlData)

create a file (be it xml or image or anything else) if you know the file format

String extension = ".xml"//or ".jpg" or anything
String filename = myfile+extension;
byte[] data = //the byte array which i got from server
File f = new File(givepathOfFile+filename );

        try {
            f.createNewFile();

            // write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(data );
            fo.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Thanks Deepak

+2
source

The byte array containing the XML is already an XML document. If you want to make this an XML file, just write the bytes to the file.

The same applies to the image file.

Are you really just asking how to write an array of bytes as a file?


Here's how to write bytes to a file in Java.

byte[] bytes = ...
FileOutputStream fos = new FileOutputStream("someFile.xml");
try {
    fos.write(bytes);
} finally {
    fos.close();
}

, finally, . , .

0

All Articles