title: "Permission denied for the attachment" (on Gmail 5.0) trying to attach file to email intent
body: <p>This question has been posted before, but there was no clear or accepted answer and all of the solutions provided that were supposed to "work" didn't for me. See here: <a href="https://stackoverflow.com/questions/26883259/gmail-5-0-app-fails-with-permission-denied-for-the-attachment-when-it-receives">Gmail 5.0 app fails with &quot;Permission denied for the attachment&quot; when it receives ACTION_SEND intent</a></p>
  
  <p>I have an app which builds up data in a text file and needs to send the text file along in an email, automatically attaching it. I have tried many ways to get this to attach, and it apparently works for Gmail 4.9 and below but 5.0 has some new permission features disabling it from doing what I wish.</p>
  
  <pre><code>    Intent i = new Intent(Intent.ACTION_SEND);
  
      String to = emailRecipient.getText().toString();
  
      i.setType("message/rfc822");
      i.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
      i.putExtra(Intent.EXTRA_SUBJECT, "Pebble Accelerometer Data");
      i.putExtra(Intent.EXTRA_TEXT, "Attached are files containing accelerometer data captured by SmokeBeat Pebble app.");
      String[] dataPieces = fileManager.getListOfData(getApplicationContext());
      for(int i2 = 0; i2 &lt; dataPieces.length; i2++){
          i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(getApplicationContext().getFilesDir() + File.separator + dataPieces[i2])));
      }
      i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(getApplicationContext().getFilesDir() + File.separator + fileManager.getCurrentFileName(getApplicationContext()))));
      Log.e("file loc", getApplicationContext().getFilesDir() + File.separator + fileManager.getCurrentFileName(getApplicationContext()));
      try {
          startActivity(Intent.createChooser(i, "Send Email"));
      } catch (android.content.ActivityNotFoundException ex) {
          Toast.makeText(Main.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
      }
  </code></pre>
  
  <p>The datapieces might be empty yes but the current file line below the for loop is always reliable and always attaches something.</p>
  
  <p>I have tried changing</p>
  
  <pre><code>Uri.fromFile()
  </code></pre>
  
  <p>to</p>
  
  <pre><code>Uri.parse()
  </code></pre>
  
  <p>When I do that, it attaches, but Gmail then crashes and when I check the logcat it's because of a null pointer. This is most likely because Gmail has no access to the file and therefore results as null.</p>
  
  <p>I've also tried using</p>
  
  <pre><code>getCacheDir()
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>getFilesDir()
  </code></pre>
  
  <p>and it has the same outcome. </p>
  
  <p>What am I doing wrong here, and how should I go about fixing it? Some example code would be <strong>really, really handy</strong> because I am new to Android development and explaining what I need to do without some sort of push off probably won't end up helping.</p>
  
  <p>Thanks a lot.</p>