0

I'm new at android development and I'm creating simple bluetooth app that can receive xml file and save xml file values to database. But how can I receive xml file from bytes array? Is it possible? After searchinf I found this question and based ont that question I try to save byte array to file. But how I need to test it? I can't find my file in my phone.

                  case Constants.MESSAGE_READ:
                    byte[] readBuffer = (byte[]) msg.obj;

                    try {
                        String path = activity.getFilesDir() + "/myFile.xml";
                        Log.d("MuTestClass", path);
                        FileOutputStream stream = new FileOutputStream(path);
                        stream.write(readBuffer);
                        stream.close();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    break;
Community
  • 1
  • 1
David
  • 3,055
  • 4
  • 30
  • 73

1 Answers1

0

You can use:

class Utils{

    public static InputStream openFile(String filename) throws IOException{
        AssetManager assManager = getApplicationContext().getAssets();
        InputStream is = null;
        is = assManager.open(filename);
        return new BufferedInputStream(is);
    }

    public static byte[] readBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];
        int len = 0;

        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        return byteBuffer.toByteArray();
    }
}

like this:

try {
     Utils.readBytes(Utils.openFile("something.xml"));
} catch (IOException e) {
     e.printStackTrace();
}
CodeNinja
  • 1,168
  • 2
  • 14
  • 27