Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Email with attachment from Asset folder

    //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?

like image 793
Ramesh sambu Avatar asked May 15 '15 06:05

Ramesh sambu


2 Answers

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.

like image 128
RonTLV Avatar answered Nov 04 '22 21:11

RonTLV


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_STREAMin 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
like image 20
Dipali Shah Avatar answered Nov 04 '22 21:11

Dipali Shah