我有一个Android应用程序,可以在应用程序的安装目录中将文本文件直接保存到手机上.我需要允许用户创建一个新的电子邮件,附加这个保存的文本文件.当我开始发送电子邮件的意图时,所有内容都会在Gmail中正确显示,但附件不会被发送.我对堆栈溢出的所有搜索似乎只涉及从SD卡附加图像文件.下面是我使用的代码.如果我做错了,请告诉我.
File myFile = new File(getFilesDir() + "/" + "someFile.txt");
FileOutputStream stream = null;
if( file != null )
{
steam = openFileOutput("someFile.txt", Context.MODE_WORLD_READABLE);
stream.write(some_data);
Uri uri = Uri.fromFile(myFile);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
sendIntent.putExtra(Intent.EXTRA_TEXT, email_text);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
file.close();
startActivity(Intent.createChooser(sendIntent, "Email:"));
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过sendIntent.setType("application/octet-stream"); 但这并没有什么不同.我为什么文件没有附加并被发送而感到茫然.
有任何想法吗?
我遇到了完全相同的问题(想要将文本文件附加到Gmail邮件).如果查看Android日志,原因是:
02-28 21:01:28.434: E/Gmail(19673): file:// attachment paths must point to file:///mnt/sdcard. Ignoring attachment file:///data/data/com.stephendnicholas.gmailattach/cache/Test.txt
Run Code Online (Sandbox Code Playgroud)
作为解决方法,您可以使用ContentProvider来访问应用程序内部缓存中的文件,以便Gmail可以附加它们.我刚刚写了一篇关于如何做的博客文章.
希望这有点帮助:)
| 归档时间: |
|
| 查看次数: |
4192 次 |
| 最近记录: |