我的应用程序可以生成我想要共享的 CSV 文件。我正在使用 MIME 类型 text/comma_separated_values/csv
,但是当我发送 Intent
时,没有显示选择器,我想我的设备不知道如何处理文件。我应该使用哪种类型?
这是我的代码:
Uri csv = lh.createDailyCSV();
if(csv == null){
Toast.makeText(this, getString(R.string.error_creating_csv), Toast.LENGTH_LONG).show();
}
else{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/comma_separated_values/csv");
sharingIntent.setData(csv);
startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.send_to)));
}
我在 list 中声明:
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/comma_separated_values/csv" />
</intent-filter>
我得到了异常(exception)
03-12 12:19:23.430: E/ActivityThread(24011): Activity com.android.internal.app.ChooserActivity has leaked IntentReceiver com.android.internal.app.ResolverActivity$1@412fc920 that was originally registered here. Are you missing a call to unregisterReceiver()?
我已经阅读了此异常,当Chooser中没有或仅1个选项时,就会发生此异常。
[编辑]
我更改了将数据附加到 Intent 的方式。我没有使用 sharingIntent.setData(csv):
sharingIntent.putExtra(Intent.EXTRA_STREAM, csv);
现在选择器工作正常,但如果我尝试通过电子邮件发送文件,我会收到错误消息:无法显示文件。
[/编辑]
最佳答案
正确的 MIME 类型应该是 text/csv
。如果这不起作用,您可以使用 text/plain
,这将允许用户从 很多 应用程序中进行选择,包括 Evernote 等。
更新 更新后,您似乎不想与任意其他应用“共享”文件而只是通过电子邮件发送?请澄清。
关于Android:共享 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15359743/