title: Picture file captured with camera intent empty for a while in onActivityResult
body: <p>I am having a very strange bug trying to take a picture via intent.
  Activity code (a little simplified):</p>
  
  <pre><code>private void startCapture() throws IOException {
      // Create output file
      final File photoFile = makePhotoFile();
      _photoPath = photoFile.getAbsolutePath();
  
      Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      /* IMPORTANT NOTE TO READERS
       * As of Android N (which went out shortly after I posted this question),
       * you cannot use Uri.fromFile this way anymore.
       * You should use a FileProvider. */
      cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
  
      if (cameraIntent.resolveActivity(getPackageManager()) != null) {
          startActivityForResult(cameraIntent, REQUEST_CODE_IMAGE_CAPTURE);
      }
      else {
          deleteCurrentPhoto();
      }
  }
  
  private File makePhotoFile() throws IOException {
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
      String imageFileName = "MyPhoto_" + timeStamp + "_";
      File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
      return File.createTempFile(imageFileName, ".jpg", storageDir);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if(requestCode == REQUEST_CODE_IMAGE_CAPTURE) {
          if(resultCode == RESULT_OK) {
              ByteArrayOutputStream bos = new ByteArrayOutputStream();
              final byte[] buffer = new byte[2048];
              int read;
              byte[] photoBytes;
              try(FileInputStream fis = new FileInputStream(_photoPath)) {
  
                  for(int i=0; i&lt;10; i++) { // Always working after first iteration though
                      if( (read = fis.read(buffer)) &lt;= 0) {
                          // Why does this get printed once ??
                          Log.w("my.package.my.app", "Empty for now...");
                      }
                      else {
                          Log.w("my.package.my.app", "Working now !");
                          bos.write(buffer, 0, read);
                          break;
                      }
                  }
  
                  while((read = fis.read(buffer)) &gt;= 0) {
                      bos.write(buffer, 0, read);
                  }
  
                  photoBytes = bos.toByteArray();
              } // Catch clauses removed for simplicity
  
              // Everything working OK after that (photoBytes decodes fine with the BitmapFactory)
          }
      }
  }
  </code></pre>
  
  <p>And the log output:</p>
  
  <blockquote>
    <p>03-01 16:32:34.139 23414-23414/my.package.my.app W/my.package.my.app:
    Empty for now... 03-01 16:32:34.139 23414-23414/my.package.my.app
    W/my.package.my.app: Working now !</p>
  </blockquote>
  
  <p>As you can see, in the onActivityResult after taking the picture, the file is empty for the first call to FileInputStream.read... And then it can be read correctly !
  I thought that when the camera intent returns to the calling activity, the file would be already written. Is there some kind of delay ? I am also surprised to see that it is always working after exactly one iteration in the for loop.</p>
  
  <p>Note that if I put a breakpoint at the beginning of onActivityResult, it delays execution a bit, and everything works normally (the file is read correctly at the first try).</p>
  
  <p>I am using a stock Nexus 6P under Android 6.0.1 with the stock camera app.</p>
  
  <hr>
  
  <p>Important edit: As for Android N, you should use a <code>FileProvider</code> instead of <code>Uri.fromFile</code> as I did before. See <a href="https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en" rel="nofollow noreferrer">this blog post</a> and <a href="https://code.google.com/p/android/issues/detail?id=203555#c3" rel="nofollow noreferrer">these explanations from the Android team</a>.</p>