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

Dipali Shah
  • 3,355
  • 28
  • 46
Ramesh sambu
  • 3,285
  • 1
  • 21
  • 37
  • `openAssetFile(Uri uri, `. Please tell the value of uri.getPath(). And uri.getLastPathSegment(). – greenapps May 15 '15 at 09:15
  • `"file/html"`. You mean "text/html". – greenapps May 15 '15 at 09:18
  • `My AssetFileDescriptor.java class file:`. No. Your custom content provider class. You could as well have mentioned in the subject trying to send a html file from assets using custom content provider. Please be more informative. – greenapps May 15 '15 at 09:21
  • https://stackoverflow.com/questions/20318573/not-able-to-attach-excel-file-from-assets-folder – Pararth Dec 04 '17 at 09:38
  • @Ramesh Sambu - please read the post : [How to copy files from assets folder to sdcard?](https://stackoverflow.com/questions/4447477/how-to-copy-files-from-assets-folder-to-sdcard) . Once you save an asset to external storage, continue with my post down below to send it as attachment. – RonTLV Dec 05 '17 at 10:48

4 Answers4

7

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.

RonTLV
  • 2,010
  • 2
  • 19
  • 31
3

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
Dipali Shah
  • 3,355
  • 28
  • 46
0

You can use following code to achieve your goal , and can change Hard coded string according your requirements .

String filename="contacts_sid.vcf"; 
File filelocation = new 
File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// Here you can set the type to 'email'
emailIntent .setType("abc.xyz.cursor.dir/email");
String to[] = {"android@gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
//  For the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Find Attachments with email");
startActivity(Intent.createChooser(emailIntent , "Sending  email..."));
-1

You can not share Asset folder file to other application without ContentProvider. You can send file which can be accesible for every application.like some whare in intenal phone memory or sd card.

Sharing Asset Check This Answer of @CommonsWare

Or Read your text file and use @RonTLV Answer and mail.

Or Copy File in intenal memory and add to sending launch. Hope this answer will help.

Imtiyaz Khalani
  • 1,915
  • 14
  • 30