如何在安卓中使用意图(Intent)发送 .text 文件?

3
我正在使用以下代码发送邮件,我需要仅通过 Gmail 发送一个 .text 文件。如何操作?请问是否有人可以帮助我吗?
Intent send = new Intent(Intent.ACTION_SENDTO);
            String uriText = "mailto:" + Uri.encode("") + 
                      "?subject=" + Uri.encode("Sample Text") + 
                      "&body=" + Uri.encode("Hi");
            Uri uri = Uri.parse(uriText);

            send.setData(uri);
            startActivity(Intent.createChooser(send, "Send mail..."));

感谢您的提前感谢。

请查看以下链接:http://stackoverflow.com/questions/5762073/problem-attaching-internal-file-to-gmail-in-my-android-app 和 http://stackoverflow.com/questions/5843834/attaching-a-file-from-secure-storage-in-gmail-from-my-app。 - NeeL
1个回答

6
File sd = Environment.getExternalStorageDirectory();
File fileDir= new File(sd, "dir_path");
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileDir.getAbsolutePath() + "/"
                                    + FILE_TXT
 startActivity(Intent.createChooser(email , "Email: Text File"));

谢谢您的回复。在这里,FILE_TEXT是什么? - suman
我发送了sample.text文件,但是我没有收到任何附件,并且我使用了email.setType("text/plain");。 - suman
我的代码是:File sd = Environment.getExternalStorageDirectory(); File fileDir= new File(sd, "dir_path"); Intent email = new Intent(Intent.ACTION_SEND); email.setType("text/plain"); email.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileDir.getAbsolutePath() + "/"+ "sample.text")); startActivity(Intent.createChooser(email , "Email: Text File")); - suman
请检查以下代码:File fileDir = new File(sd, "dir_path"); 它应该指向文件所在的实际目录路径。 - Tarun

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接