title: Hello-World of FileProvider
body: <p><strong>This question contained several sub-questions. I am forking these, starting by <a href="https://stackoverflow.com/questions/19032266/sharing-a-simple-text-file-in-android">this question</a>. I'll eventually clean up by deleting this question.</strong></p>
  
  <p>The following program will in theory share a hello-world text file. The code runs, but sharing to either Dropbox or to Gmail (by way of just two concrete examples) fails.</p>
  
  <pre><code>public class MainActivity extends Activity {
      @Override protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          String filename = "hellow.txt";
          String fileContents = "Hello, World!\n";
          byte[] bytes = fileContents.getBytes();
          FileOutputStream fos = null;
          try {
              fos = this.openFileOutput(filename, MODE_PRIVATE);
              fos.write(bytes);
          } catch (IOException e) {                       
              e.printStackTrace();
          } finally {
              try {
                  fos.close();
              } catch (IOException e) {                       
                  e.printStackTrace();
              }
          } 
  
          File file = new File(filename);
          Intent shareIntent = new Intent();
          shareIntent.setAction(Intent.ACTION_SEND);
          shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
          shareIntent.setType("application/txt");
          startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
  
          file.delete();
      }
  }
  </code></pre>
  
  <p>Aside from adding a value for <code>send_to</code> in res/values/strings.xml, the only other pair of changes I did to the generic <code>Hello, World</code> that Eclipse creates is adding the following <code>&lt;provider&gt;</code> tag in AndroidManifest.xml:</p>
  
  <pre><code>&lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
  
      &lt;provider
          android:name="android.support.v4.content.FileProvider"
          android:authorities="com.mycorp.helloworldtxtfileprovider.MainActivity"
          android:exported="false"
          android:grantUriPermissions="true" &gt;
          &lt;meta-data
              android:name="android.support.FILE_PROVIDER_PATHS"
              android:resource="@xml/my_paths" /&gt;
      &lt;/provider&gt;
  
      &lt;activity
          android:name="com.mycorp.helloworldtxtfileprovider.MainActivity"
          ...
  </code></pre>
  
  <p>... and adding the following in res/xml/my_paths.xml</p>
  
  <pre><code>&lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;files-path name="files" path="." /&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>My main question is the first, but while you're at this topic, a discussion of questions 2-4 would also be interesting.</p>
  
  <ol>
  <li>Why does the program above fail?</li>
  <li>Is it indeed the case that if one needs a custom <code>ContentProvider</code>, then one needs to extend that class, but if one just needs a <code>FileProvider</code>, then one can use that class without derivation?</li>
  <li>In this code, I needed to use <code>filename</code> twice—once with <code>openFileOutput</code> and another with <code>new File()</code>. Is there a way to avoid this duplication (that would guarantee that the same file is being referenced)?</li>
  <li>Is it safe to delete the file right after <code>startActivity(..)</code> is called, or is it necessary to devise a callback to wait learning that the file has been uploaded/shared. (The real file may take some time to share/upload.)</li>
  </ol>
  
  <p><strong>Edit</strong></p>
  
  <p>The code runs fine and shows a list of apps to send to.</p>
  
  <p>If I select Dropbox, I can select the location just fine. Dropbox sends the notifications "Uploading to Dropbox" followed by "Upload failed: my_file.txt".</p>
  
  <p>If I select Gmail, I can fill the recipient and the file appears to be attached, but after "sending message.." I get "Couldn't send attachment".</p>