Email a text file attachment

I am trying to attach a text file to send it via email but whenever I open my email application it says the file does not exist. Help me please.

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"musabyahya1005@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT   , "body of email");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(directory+"/data.txt"));

try {
     startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
     Toast.makeText(getBaseContext(), "An Error Happened ", Toast.LENGTH_SHORT).show();
}

      

+3


source to share


1 answer


If you are trying to send files by email, make sure they are in a publicly accessible location and you tell them that this is the file you want to send. Use this code as an example.



Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("message/rfc822").putExtra(Intent.EXTRA_EMAIL, new String[]{"fake@example.com"}).putExtra(android.content.Intent.EXTRA_SUBJECT, "Mail subject").putExtra(android.content.Intent.EXTRA_TEXT, "lalalala");
String targetFilePath = Environment.getExternalStorageDirectory().getPath() + File.separator + "tmp" + File.separator + "test.txt";
Uri attachmentUri = Uri.parse(targetFilePath);
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentUri)); 

      

+4


source







All Articles