从资产文件夹发送带有附件的电子邮件

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"));

最后,我从资产文件夹(组合-1.html)获取文件。

正在

Runtime error file not found exception.

还有其他方式发送文件附件吗?

发送电子邮件最简单的方法是创建类型为 ACTION_SEND 的 Intent:

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");

要附加单个文件,请向 Intent 添加一些扩展数据:

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/path/to/file")));
intent.setType("text/html");

或使用Html.fromHtml()构建html内容:

intent.putExtra( Intent.EXTRA_TEXT,
                 Html.fromHtml(new StringBuilder()
                 .append("<h1>Heading 1</h1>")
                 .append("<p><a>http://www.google.com</a></p>")
                 .toString()));

对于多个附件:

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);

调用 startActivity() 传递 intent。

为您的资产文件夹文件创建一个 File 对象,并将该对象附加到您的电子邮件 Intent 中。

并且如您的问题所述未找到运行时错误文件异常这可能是导致URL"file:///android_asset/" 不指向特定目录,它仅供 WebView 用于寻址资产。拉那个from

您可以将其作为输入流打开并将其 InputStream 转换为 File

in = new BufferedReader(new InputStreamReader(activity.getAssets().open(myfile.pdf))); 

按如下方式在电子邮件中发送此文件对象。

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);

其中 Intent.ACTION_SEND 用于发送电子邮件,Intent.EXTRA_STREAM 用于电子邮件附件。您可以在一个意图中有多个 Intent.EXTRA_STREAM,以使用 intent.setAction(Intent.ACTION_SEND_MULTIPLE); 引用多个附件。

intent.setType(String mimeType) 输入参数表示您希望从触发意图(这里是意图实例)中获取的 MIME 类型数据 return。其中 setype 可以是

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

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..."));

如果没有 ContentProvider,您无法将 Asset 文件夹文件共享到其他应用程序。您可以发送内部 phone 内存或 SD 卡中每个 application.like 一些 whare 都可以访问的文件。

共享资产Check This Answer of @CommonsWare

或者 阅读您的文本文件并使用@RonTLV 回复和邮件。

或者 复制内存中的文件并添加到发送发射。 希望这个回答对你有所帮助。