我希望能够创建一个包含数据的文本文件,通过电子邮件发送该文件,然后在我的Android设备上删除它。
以下是我到目前为止所得到的错误:
java.io.FileNotFoundException: /20150719_130219: open failed: EROFS (Read-only file system)
代码:
BufferedWriter writer = null;
try {
//create a temporary file
String report = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
File logFile = new File(report);
writer = new BufferedWriter(new FileWriter(logFile));
for (Map.Entry entry : cbh.data.entrySet()) {
reportText.append(entry.getKey() + ", " + entry.getValue() + "\n");
writer.write(entry.getKey() + ", " + entry.getValue() + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
}
}
答案 0 :(得分:1)
首先,您需要在清单文件中提供权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在您的代码中,您只是生成文件的名称,但未定义其完整路径。
Environment.getExternalStorageDirectory()
将返回sdcard的路径
你可以写这个
String report = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
String fileName = Environment.getExternalStorageDirectory()+ report+".txt";
File file = new File(fileName);