写入文件不会写

时间:2015-05-23 20:17:58

标签: java android file email file-io

正如标题所述,我正在创建一个新文件,然后将该文件作为电子邮件附件发送

文件创建代码:

newFile = new File("/sdcard/" + TEMPFILENAME);
if (newFile.exists()) {
    newFile.delete();
}
newFile.createNewFile();
for (int j = 0; j < arrayList.size(); j++) {
    writeFileToStorage(TEMPFILENAME, arrayList.get(j).AccountName, arrayList.get(j).UserName, encryptString(arrayList.get(j).Password));
}
recipient = emailEdit.getText().toString();
new EmailSender().execute();

然后在我的电子邮件服务类中,我有以下

MimeMessage message = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file.getAbsolutePath());
DataHandler handler = new DataHandler(source);

message.setSender(new InternetAddress(sender));
message.setSubject(subject);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

messageBodyPart.setDataHandler(handler);
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);
Transport.send(message);

使用正确的名称和正确的收件人/发件人发送文件,但文件始终为空。

有什么想法吗?

编辑:

writeFileToStorage的实现,在保存数据时定期在应用程序中运行

try {
    bufferedWriter = new BufferedWriter(new OutputStreamWriter(openFileOutput(fName, MODE_APPEND)));
    bufferedWriter.write(aName);
    bufferedWriter.newLine();
    bufferedWriter.write(_uName);
    bufferedWriter.newLine();
    bufferedWriter.write(_pWord);
    bufferedWriter.newLine();
    bufferedWriter.close();
    Log.d("Wrote: ", ("AccountName: "+ aName + " UserName: " + _uName + " Password: " + _pWord+ " to: " + fName));
} catch (Exception err) {
    String writeError = getString(R.string.write_error);
    Toast.makeText(getBaseContext(), writeError, Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:0)

如何编写writeFileToStorage(...)方法会很有趣。

致电newFile.createNewFile();后,您的newFile将不会被操纵。

编辑:您的writeFileToStorage(...)方法似乎无效。参数fname不正确。