74

Possible Duplicate:
Email from internal storage

The email is being received on by the recipient, but without the attachment. Here is the code, any expert knows where did I go wrong?

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
File file = new File(root, xmlFilename);
if (!file.exists() || !file.canRead()) {
    Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
    finish();
    return;
}
Uri uri = Uri.parse("file://" + file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send email..."));

I am not getting any toast message. Thanks.

Community
  • 1
  • 1
Mr Jackson
  • 1,023
  • 2
  • 10
  • 11
  • I think that the other question is why email can't be sent from internal storage. And in this one it does not work when being sent from external storage. While similar, they are not the same. – Alex Gitelman May 21 '11 at 02:11
  • 1
    @Alex Gitelman actually the failure mode is the same, and he was provided an answer over there which will work for internal _or_ external storage. – Chris Stratton May 21 '11 at 03:00
  • this line does not create a file (well known in java) File file = new File(root, xmlFilename); you have to create an output stream writer using the file instance to create the file, unless the file already exists on the file system. – Khaled Annajar Feb 19 '13 at 08:00
  • I found this thread very useful in my case https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed – cwhsu Jul 26 '20 at 10:19

1 Answers1

16

The file is probably not world readable.

EDIT: indeed. Try doing this:

Uri uri = Uri.parse("file://" + file.getAbsolutePath());
Femi
  • 64,273
  • 8
  • 118
  • 148
  • i tried it already. same result - you've got to help me :( – Mr Jackson May 20 '11 at 22:37
  • Don't you already have a question on this today? – Chris Stratton May 20 '11 at 22:49
  • 1
    Do you expect us to believe that this is a _different_ mr jackson? http://stackoverflow.com/questions/6072895/email-from-internal-storage/6074783#6074783 – Chris Stratton May 20 '11 at 23:30
  • Ah. Well, can't really think of any other reason: I'm guessing you should double check your permissions. I have this exact code and it works just fine in multiple deployed apps. – Femi May 21 '11 at 02:08
  • @chris, when I was saving internally, you told me to save externally. I did it, but I get an error. It is not the same, though the same error arises. – Mr Jackson May 21 '11 at 06:30
  • 5
    **I** did not tell you to save externally, I **fixed** your code for sending an attachment from internal storage so that it now works around the gmail oddity. Rather than start over with a new program with new mistakes, please take your old program from your other question in your other account, and use it, either with internal or external storage. – Chris Stratton May 21 '11 at 06:33
  • I'd do what @Chris says: if the `logcat` output is showing GMail requiring the `/mnt/sdcard` path then that would be the way to do it: `Uri uri = Uri.fromFile(new File("/mnt/sdcard/../.."+file.getAbsolutePath()));` – Femi May 21 '11 at 06:43
  • 1
    If all else fails you could always implement your own `ContentProvider` that just passed the file data through: I've used that successfully to attach files to GMail. – Femi May 21 '11 at 06:45
  • 2
    Yes, either make sure the path you are seeing in logcat is valid, and that it begins with /mnt/sdcard (even if it ends up elsewhere, to keep GMail happy) or learn about ContentProviders. And please stick to one account and one question per topic in the future. – Chris Stratton May 21 '11 at 06:49