title: Create and Share a File from Internal Storage
body: <p>My goal is to create a XML file on <strong>internal storage</strong> and then send it through the share Intent.</p>
  
  <p>I'm able to create a XML file using this code</p>
  
  <pre><code>FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
  PrintStream printStream = new PrintStream(outputStream);
  String xml = this.writeXml(); // get XML here
  printStream.println(xml);
  printStream.close();
  </code></pre>
  
  <p>I'm stuck trying to retrieve a Uri to the output file in order to share it. I first tried to access the file by converting the file to a Uri</p>
  
  <pre><code>File outFile = context.getFileStreamPath(fileName);
  return Uri.fromFile(outFile);
  </code></pre>
  
  <p>This returns <em>file:///data/data/com.my.package/files/myfile.xml</em> but I cannot appear to attach this to an email, upload, etc.</p>
  
  <p>If I manually check the file length, it's proper and shows there is a reasonable file size.</p>
  
  <p>Next I created a content provider and tried to reference the file and it isn't a valid handle to the file. The <code>ContentProvider</code> doesn't ever seem to be called a any point.</p>
  
  <pre><code>Uri uri = Uri.parse("content://" + CachedFileProvider.AUTHORITY + "/" + fileName);
  return uri;
  </code></pre>
  
  <p>This returns <em>content://com.my.package.provider/myfile.xml</em> but I check the file and it's zero length.</p>
  
  <p>How do I access files properly? Do I need to create the file with the content provider? If so, how?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Here is the code I'm using to share. If I select Gmail, it does show as an attachment but when I send it gives an error <strong>Couldn't show attachment</strong> and the email that arrives has no attachment.</p>
  
  <pre><code>public void onClick(View view) {
      Log.d(TAG, "onClick " + view.getId());
  
      switch (view.getId()) {
          case R.id.share_cancel:
              setResult(RESULT_CANCELED, getIntent());
              finish();
              break;
  
          case R.id.share_share:
  
              MyXml xml = new MyXml();
              Uri uri;
              try {
                  uri = xml.writeXmlToFile(getApplicationContext(), "myfile.xml");
                  //uri is  "file:///data/data/com.my.package/files/myfile.xml"
                  Log.d(TAG, "Share URI: " + uri.toString() + " path: " + uri.getPath());
  
                  File f = new File(uri.getPath());
                  Log.d(TAG, "File length: " + f.length());
                  // shows a valid file size
  
                  Intent shareIntent = new Intent();
                  shareIntent.setAction(Intent.ACTION_SEND);
                  shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                  shareIntent.setType("text/plain");
                  startActivity(Intent.createChooser(shareIntent, "Share"));
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              }
  
              break;
      }
  }
  </code></pre>
  
  <p>I noticed that there is an <code>Exception</code> thrown here from inside <em>createChooser(...)</em>, but I can't figure out why it's thrown.</p>
  
  <blockquote>
    <p>E/ActivityThread(572): Activity
    com.android.internal.app.ChooserActivity has leaked IntentReceiver
    com.android.internal.app.ResolverActivity$1@4148d658 that was
    originally registered here. Are you missing a call to
    unregisterReceiver()?</p>
  </blockquote>
  
  <p>I've researched this error and can't find anything obvious. Both of these links suggest that I need to unregister a receiver.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/11308260/chooser-activity-leak-android">ChooserActivity has leaked IntentReceiver</a></li>
  <li><a href="https://stackoverflow.com/questions/10068954/why-does-intent-createchooser-need-a-broadcastreceiver-and-how-to-implement">Why does Intent.createChooser() need a BroadcastReceiver and how to implement?</a></li>
  </ul>
  
  <p>I have a receiver setup, but it's for an <code>AlarmManager</code> that is set elsewhere and doesn't require the app to register / unregister.</p>
  
  <p><strong>Code for openFile(...)</strong></p>
  
  <p>In case it's needed, here is the content provider I've created.</p>
  
  <pre><code>public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
      String fileLocation = getContext().getCacheDir() + "/" + uri.getLastPathSegment();
  
      ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(fileLocation), ParcelFileDescriptor.MODE_READ_ONLY);
      return pfd;
  }
  </code></pre>