gpt4 book ai didi

java - Android - 如何在不使用内容提供商的情况下将文件附加到邮件

转载 作者:行者123 更新时间:2023-11-30 09:14:20 25 4
gpt4 key购买 nike

您好,我正在尝试创建一个应用程序来创建一个 word 文件并通过邮件发送它。到目前为止,我设法创建了文件,以及调用邮件应用程序选择器的必要 Intent ,但是我遇到了两个问题:

1)我收到一个列表,其中包含的不仅仅是邮件程序——还有 wifi、BT 等

2) 如果我选择 gmail,请在 gmail 中将文件名作为附件查看,但是发送后我没有收到任何文件。我试图切换到使用 URI 而不是 Uri,但是当 gmail 尝试附加文件时我得到了 javaNullExeption。

问题是:如何发送附件(尽可能不使用内容提供者等复杂的类)

我使用的代码:

创建文件:

public void saveFile(String fileName,String content) throws IOException,FileNotFoundException
{
FileOutputStream fos = getContext().openFileOutput(fileName, Context.MODE_WORLD_READABLE);
fos.write(content.getBytes());
fos.close();
}

发送:

public void sendAsMail(Context context,String fileName)
{
File file = new File(getContext().getFilesDir().getAbsolutePath()+"/"+fileName);
//file.setReadable(true);
//URI myUri = file.toURI();
Uri myUri=Uri.fromFile(file);
Intent emailIntent = new Intent (Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL,"");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, context.getResources().getString(R.string.free_search_mail_subject));
emailIntent.putExtra(Intent.EXTRA_TEXT,context.getResources().getString(R.string.free_search_mail_content));
emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
context.startActivity(Intent.createChooser(emailIntent, "Send the file using:"));
}

我测试了文件以查看它是否正确创建(使用扫描仪打印)并且它似乎不是问题

最佳答案

首先检查文件是否创建。如果已创建,请尝试使用此代码:

public static void sendAsMail(File file, Context econtext) {
try {
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("text/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { "" });
emailIntent.putExtra(android.content.Intent.EXTRA_CC,
new String[] {});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"FROM Sample");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "HI");
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,
Uri.parse(file.toURI().toString()));
mContext.startActivity(emailIntent);
} catch (Exception e) {
}
}

如果不起作用,请发表评论。

关于java - Android - 如何在不使用内容提供商的情况下将文件附加到邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com