【问题标题】:Android bluetooth client receive xmlAndroid蓝牙客户端接收xml
【发布时间】:2015-10-21 18:07:00
【问题描述】:

我是 android 开发的新手,我正在创建简单的蓝牙应用程序,它可以接收 xml 文件并将 xml 文件值保存到数据库。但是如何从字节数组接收 xml 文件?可能吗?在 searchinf 之后,我找到了 this question 并基于这个问题,我尝试将字节数组保存到文件中。但是我需要如何测试呢?我在手机中找不到我的文件。

                  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;

【问题讨论】:

    标签: android xml bluetooth


    【解决方案1】:

    你可以使用:

    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();
        }
    }
    

    像这样:

    try {
         Utils.readBytes(Utils.openFile("something.xml"));
    } catch (IOException e) {
         e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-01
      • 1970-01-01
      • 2013-06-21
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2013-08-14
      • 2016-09-18
      相关资源
      最近更新 更多