//EMAIL SENDING CODE FROM ASSET FOLDER
email = editTextEmail.getText().toString();
subject = editTextSubject.getText().toString();
message = editTextMessage.getText().toString();
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("file/html");
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.example.deepa.xmlparsing/file:///android_assets/Combination-1.html"));
startActivity(Intent.createChooser(emailIntent, "Send email using"));
Finally, I'm getting the file from asset folder (Combination-1.html).
It is getting
Runtime error file not found exception.
Is there any other way to send file attachment?
The easiest way to send an email is to create an Intent of type ACTION_SEND :
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
intent.putExtra(Intent.EXTRA_TEXT, "Attachment test");
To attach a single file, add some extended data to Intent :
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/path/to/file")));
intent.setType("text/html");
Or use Html.fromHtml() to build html content:
intent.putExtra( Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<h1>Heading 1</h1>")
.append("<p><a>http://www.google.com</a></p>")
.toString()));
For multiple attachments:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple");
intent.putExtra(Intent.EXTRA_TEXT, "multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(new File("/path/to/first/file")));
uris.add(Uri.fromFile(new File("/path/to/second/file")));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
Finish with a call to startActivity() passing intent.
Create a File object of your asset folder file and attach this object to your email Intent.
And as mentioned in your Question runtime error file not found exception this may be cause the URL "file:///android_asset/" doesn't point to a particular directory, it is only used by WebView to address assets. Pulled that from
you can open this as an input stream and covert this InputStream
to File
in = new BufferedReader(new InputStreamReader(activity.getAssets().open(myfile.pdf)));
Send this file object in email as follow.
Intent intent = new Intent(Intent.ACTION_SEND ,Uri.parse("mailto:"));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
Where Intent.ACTION_SEND
used to send Email , Intent.EXTRA_STREAM
for the attachments with email. you can have multiple Intent.EXTRA_STREAM
in single intent to refer multiple attachments with intent.setAction(Intent.ACTION_SEND_MULTIPLE);
.
intent.setType(String mimeType)
input param is represent the MIME type data that you want to get in return from firing intent(here intent instance).where setype can be
image/jpeg
audio/mpeg4-generic
text/html
audio/mpeg
audio/aac
audio/wav
audio/ogg
audio/midi
audio/x-ms-wma
video/mp4
video/x-msvideo
video/x-ms-wmv
image/png
image/jpeg
image/gif
.xml ->text/xml
.txt -> text/plain
.cfg -> text/plain
.csv -> text/plain
.conf -> text/plain
.rc -> text/plain
.htm -> text/html
.html -> text/html
.pdf -> application/pdf
.apk -> application/vnd.android.package-archive
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With