0

I'm working on creating an app but I'm having a problem. After I've inserted some data in a file named "message.txt", I'm not able to send this file by e-mail because my smartphone says "impossible to send the attachment". How can I resolve this problem?

This is the code:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String address = ...
    byte[] messagetosend = ...
    String filename = "message.txt";
    File file = new File(getExternalFilesDir(null), filename);
    FileOutputStream outputstream;
    try {
        outputstream = openFileOutput(filename,MODE_WORLD_READABLE);
        outputstream.write(messagetosend);
        outputstream.flush();
        outputstream.getFD().sync();
        outputstream.close();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{address});
    sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    sendIntent.setType("*/*");
    startActivity(Intent.createChooser(sendIntent,"Send with..."));
    }

2 Answers2

0

Third party apps have no rights to access that file. Use FileProvider to serve it, or copy the file to external storage instead of internal storage.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Trying http://www.droidviews.com/how-to-setfix-file-permissions-on-android-devices-a-beginners-guide/ will help?Never done that.. – xyz May 12 '15 at 16:24
  • @xyz: That blog post has nothing to do with app development. – CommonsWare May 12 '15 at 16:41
  • I've tried writing the file out to external storage and Gmail (for example) recognize the attachment, but when I click on the button to Send the e-mail, a notification appears on the notification bar that says "Impossible to send the attachment". Do you kno why? – Paolo Guizzardi May 12 '15 at 16:53
  • @PaoloGuizzardi: Since I cannot see your code for this, I do not really know. If nothing else, call `flush()`, then `getFD().sync()`, and then `close()` on your `FileOutputStream`, not just `close()`, to ensure all bytes are written to disk before you try invoking some other app to work with that file. – CommonsWare May 12 '15 at 16:56
  • To the code shown above, I've added this calls you told me and modified "getFilesDir" with "getExternalFilesDir". Now Gmail reads the file but says me "Impossible to send the attachment". I don't really know why. – Paolo Guizzardi May 12 '15 at 17:10
  • @PaoloGuizzardi: Hmmmm... if you are still using `Uri.fromFile()`, I would expect Gmail to be reasonably happy with what you describe. Are you sure that you're not having a problem in your `try`/`catch` that is just quietly being dumped somewhere? I never use `e1.printStackTrace();`, but instead use `Log.e()` to ensure the exception makes it out at error severity. Also, in your case, you don't want to go through the `startActivity()` stuff if that disk I/O fails. – CommonsWare May 12 '15 at 17:17
0

If you have saved the file as being private to the app, the app can see if fine but the external email client will not be able to see it. You'll need to write it out to external storage, or make it public. visit this link and visit this you will understand about access levels.

OR try to send email with JavaMail here is solved, here is too

Community
  • 1
  • 1
Sajidkhan
  • 608
  • 7
  • 16
  • I've tried writing the file out to external storage and Gmail (for example) recognize the attachment, but when I click on the button to Send the e-mail, a notification appears on the notification bar that says "Impossible to send the attachment". Do you kno why? – Paolo Guizzardi May 12 '15 at 16:52
  • do you realy need to do this type of work using intent? if you want to send the attachment via Gmail only then use java lib for sending email. – Sajidkhan May 12 '15 at 17:11
  • i want to send the attachment via gmail or any other app of mail sending. i know only this method to do this. can you link me the java lib? – Paolo Guizzardi May 12 '15 at 17:16
  • if your problem is solved. accept answer OR let me know if you need more help. – Sajidkhan May 12 '15 at 17:25
  • Sorry but I need to use the Intent. I've updated the code with the last code. Can you see if there is any other problem? – Paolo Guizzardi May 12 '15 at 17:43
  • in you try/catch block print the exception if to know about the file status. – Sajidkhan May 12 '15 at 19:16