【发布时间】:2013-12-09 04:38:51
【问题描述】:
嗯,首先,我只是在学习,不太明白我在做什么。 我想要的是在内存中创建一个 Excel 文件,然后可以将其与 ActionBarSherlock 的 ShareActionProvider 一起发送到邮件中。
但我得到了例外:
11-24 18:45:52.112: W/System.err(22073): java.io.FileNotFoundException: /Competition.xls: open failed: EROFS (Read-only file system)
当我在网上搜索答案时 - 这是在只读系统区域中创建文件的问题。但我想在内存中创建它..不知何故。再一次,我不太明白它是如何工作的——我看到它的方式——我在内存的某个地方创建了 .xls 文件。所以解释会很有帮助。
所以,这里是代码:
private void createFileTosend() {
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
File toSend=null;
try {
toSend = getFile();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inputStream = new BufferedInputStream(new FileInputStream(toSend));
outputStream = openFileOutput("Competition.xls",
Context.MODE_WORLD_READABLE | Context.MODE_APPEND);
byte[] buffer = new byte[1024];
int length = 0;
try {
while ((length = inputStream.read(buffer)) > 0){
outputStream.write(buffer, 0, length);
}
} catch (IOException ioe) {
/* ignore */
}
} catch (FileNotFoundException fnfe) {
/* ignore */
} finally {
try {
inputStream.close();
} catch (IOException ioe) {
/* ignore */
}
try {
outputStream.close();
} catch (IOException ioe) {
/* ignore */
}
}
}
public File getFile() throws IOException, WriteException{
File file=new File("Competition.xls");
WritableWorkbook workbook = Workbook.createWorkbook(file);
//then goes creation of Excel 's xls file which is not important for the question
workbook.write();
workbook.close();
return file;
}
再一次,请不要对我投反对票,我只是在学习
【问题讨论】:
-
我很确定您需要在共享之前将其保存到磁盘。您无法访问某种系统范围的“内存”,您只能在其中放置其他应用可以访问的内容。
-
@KenWolf - 实际上,这是不正确的。 Android 支持文字共享内存,以及一些通过 Intent 传递数据结构的方式。但通常这是通过在外部存储上创建文件来完成的。
-
@ChrisStratton 很有趣,我将不得不查找文字共享内存(这一定有一些安全隐患?)。基本上我看不到如何在不写入磁盘的情况下创建 Excel 文件并与其他应用程序共享,但也许我误解了这个要求。
-
我同意将它写入磁盘是可取的 - 大多数目标应用程序将无法接受它。我只是指出实际上允许共享内存映射,并且还有其他机制,例如要传递的序列化对象或传递类似回调的内容提供程序,理论上可以在合作开发的应用程序之间使用。
-
@user2976267 文件路径是什么?
标签: java android file inputstream