This question was published earlier, but there was no clear or accepted answer, and all the decisions that were supposed to βworkβ were not for me. See Here: The Gmail 5.0 application failed with "Permission denied for attachments" when it receives the intent ACTION_SEND
I have an application that creates data in a text file and should send the text file by email, automatically attaching it. I tried many ways so that this can be attached, and apparently this works for Gmail 4.9 and lower, but 5.0 has some new permission features that disable it from doing what I want.
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 < 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(); }
The data may be empty yes, but the current line of the file below the for loop is always reliable and always gives something.
I tried to change
Uri.fromFile()
to
Uri.parse()
When I do this, it joins, but Gmail then crashes and when I check logcat due to a null pointer. Most likely, this is due to the fact that Gmail does not have access to the file and therefore is null.
I also tried using
getCacheDir()
instead
getFilesDir()
and has the same result.
What am I doing wrong here, and how do I fix it? Some sample code would be really, really convenient , because I'm new to Android development and explaining what I need to do without any repulsion probably won't help.
Many thanks.
android android-intent permissions gmail
Edwin finch
source share