Java 使用bytestream使用共享意图将excel工作表添加到电子邮件
我尝试使用以下代码在android/java中创建并发送excel工作表,但无法完成将ByteArrayDataSource添加到intent的正确语法代码:Java 使用bytestream使用共享意图将excel工作表添加到电子邮件,java,android,excel,android-intent,Java,Android,Excel,Android Intent,我尝试使用以下代码在android/java中创建并发送excel工作表,但无法完成将ByteArrayDataSource添加到intent的正确语法代码: Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(android.content.Intent.EXTR
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Your excel sheet");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Hello! I am sending the excel sheet");
Workbook xlsFile = new HSSFWorkbook();
CreationHelper helper = xlsFile.getCreationHelper();
Sheet sheet1 = xlsFile.createSheet("New worksheet");
/* fill excel sheet */
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
xlsFile.write(bos); // write excel data to a byte array
DataSource fds = new ByteArrayDataSource(bos.toByteArray(), "application/vnd.ms-excel");
bos.close();
sharingIntent.putExtra(Intent.EXTRA_STREAM, ??? /* how to add the ByteArrayDataSource here?? */);
} catch (IOException e) {
e.printStackTrace();
}
startActivity(Intent.createChooser(sharingIntent, "Send via"));
如何附加excel工作表而不将其存储在SD卡上
谢谢大家!
致以最良好的祝愿,
尤尔根
更新:
我通过先存储到SD卡来完成:
try {
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard, "file.xls");
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
xlsFile.write(bos);
fo.write(bos.toByteArray());
bos.close();
fo.flush();
fo.close();
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + file.getPath()));
} catch (IOException e) {
e.printStackTrace();
}
如果有人知道一种不用储存的方法,请告诉我!谢谢
我尝试使用以下代码在android/java中创建并发送excel工作表
首先,您的MIME类型错误,因为text/plain
不是Excel电子表格的MIME类型
第二,你可以
如何附加excel工作表而不将其存储在SD卡上
将其存储在内部存储器中,并提供给第三方应用程序。文本/纯文本用于电子邮件正文。因此,如果不先存储,我就无法添加excel工作表?@Juwei:“text/plain代表电子邮件的正文”--引用文档:“Input:getType()是发送数据的MIME类型。get*Extra可以有一个额外的文本或额外的流字段,其中包含要发送的数据。如果使用额外的文本,MIME类型应该是“text/plain”"; 否则,它应该是EXTRA_STREAM中数据的MIME类型。“您不能同时使用
EXTRA_TEXT
和EXTRA_STREAM
。请选择一个。然后,将MIME类型设置为文档所示。@Juwei:“所以我不能添加excel工作表而不先存储它?”?--您可能没有堆空间来执行任何其他操作,因为Excel文件可能很大。应该可以创建一个从堆空间提供服务的ContentProvider
,尽管我从未尝试过。我是通过先存储到SD卡来实现的。请参阅我的更新请求。如果有人知道一种不用储存的方法,请告诉我!谢谢