title: Image sent over Wi-Fi Direct is corrupted
body: <p>I'm trying to follow <a href="https://developer.android.com/guide/topics/connectivity/wifip2p" rel="nofollow noreferrer">this tutorial from the android docs</a> which explains how to send images over Wi-Fi direct. I essentially just copied what was there and instead of the image, it sends a corrupted jpeg of the same size. The data is sent fine and it is received on the other end, but it still produces a corrupted jpeg. The images which I am sending are definitely jpeg format as well. <a href="https://i.stack.imgur.com/11hmn.png" rel="nofollow noreferrer">This is what the image data stream looks like when it is received on the second device.</a></p>
  <p>Most of this was taken from the documentation so I don't see why it wouldn't work, but when I view the file on Android, it gets stuck loading and on Windows, it says that &quot;File format not supported&quot;.</p>
  <p>This is the code responsible for writing the image to the <code>OutputStream</code>.</p>
  <pre><code>        OutputStream tmpOut = null;
  
          try {
  
              OutputStream outputStream = socket.getOutputStream();
              ContentResolver cr = MainActivity.getContentResolver();
              InputStream inputStream = null;
              inputStream = cr.openInputStream(myUri);
              while ((len = inputStream.read(buffer)) != -1) {
                  outputStream.write(buffer, 0, len);
              }
  
  
  
          } catch (IOException e) {
              e.printStackTrace();
          }
  
  
      }
  </code></pre>
  <p>This is the code responsible for receiving the bytes of the image files and creating a file from it.</p>
  <pre><code>                bytes = inputStream.read(buffer);
  
                  tempMsg = new String(buffer);
  
                  if ((tempMsg = null)) {
                      ...
                  } else if (!isASCII(tempMsg)) {
                      final File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + &quot;mage.jpg&quot;);
  
                      File dirs = new File(f.getParent());
                      if (!dirs.exists())
                          dirs.mkdirs();
                      f.createNewFile();
  
                      copyFile(inputStream, new FileOutputStream(f));
  </code></pre>