title: Android: How to save the camera result to a private file
body: <p>I am trying to get an image from the camera and save it directly to my app's private files directory. For security concerns, the image should not be publicly accessible at any time. </p>
  
  <p>In general, the way you grant temporary access to a private file is to use a ContentProvider and set the GRANT_WRITE_URI_PERMISSION flag in the Intent. Following the documentation in <a href="http://developer.android.com/reference/android/support/v4/content/FileProvider.html">FileProvider</a>, I have done the following:</p>
  
  <p><strong>AndroidManfiest.xml</strong>
  </p>
  
  <pre><code>&lt;manifest&gt; 
      ...
      &lt;application&gt;
          ...
          &lt;provider
              android:authorities="com.my.domain"
              android:name="android.support.v4.content.FileProvider"
              android:exported="false"
              android:grantUriPermissions="true"&gt;
              &lt;meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/file_paths" /&gt;
          &lt;/provider&gt;
          ...
  </code></pre>
  
  <p><strong>res/xml/file_paths.xml</strong>
  </p>
  
  <pre><code>&lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;files-path name="my_images" path="images/"/&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>When launching the camera activity, I execute the following from an activity:
  </p>
  
  <pre><code>File imageFile = getInternalImageFile();
  Uri captureUri = FileProvider.getUriForFile(this, "com.my.domain", imageFile);
  
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  
  // grant temporary access to the file
  intent.setData(captureUri);
  intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  
  // tell the camera where to save the file
  intent.putExtra(MediaStore.EXTRA_OUTPUT, captureUri);
  
  startActivityForResult(intent, IMAGE_CAPTURE_REQUEST_CODE);
  </code></pre>
  
  <p>This however results in the camera app returning immediately without doing anything. I suspect because it's not expecting there to be any data set on intent (Intent.setData()).</p>
  
  <p>The above strategy isn't working out so well. So, how can I securely save an image captured from the camera directly to my app's private files directory?</p>