2016-05-31 165 views
0

我正在开发一个Android应用程序,用于将文件从一台设备发送到另一台。 建立两个设备之间的连接完美,但传输文件时出现问题。 在接收设备上,文件被创建,但不幸的是它是空的。Android - 通过蓝牙发送文件

这是我处理输入文件代码:

try { 
    byte[] buffer = new byte[1024]; 
    int bytes = 0; 
    boolean eof = false; 

    File file = new File(Environment.getExternalStoragePublicDirectory(
      Environment.DIRECTORY_PICTURES), "test.jpg"); 
    OutputStream os = null; 
    try { 
     os = new BufferedOutputStream(new FileOutputStream(file)); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    while (!eof) { 
     bytes = mmInStream.read(buffer); 
     int offset = bytes - 11; 
     byte[] eofByte = new byte[11]; 
     eofByte = Arrays.copyOfRange(buffer, offset, bytes); 
     String message = new String(eofByte, 0, 11); 

     if(message.equals("end of file")) { 
      os.flush(); 
      os.close(); 

      eof = true; 
     } else { 
      os.write (buffer); 
     } 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

回答

0

使用DataInputStream类/ DataOuputStream解决了这个问题。