【发布时间】:2011-12-05 12:02:32
【问题描述】:
我正在尝试一些简单的事情,例如创建一个文本文件,然后将其作为附件发送。虽然如果我使用 sdcard 可以正常工作,但我不知道将它放在“标准数据文件夹”的哪个位置,所以我的应用实际上适用于没有 sdcard 的每个人(并且该文件有些不可见)
虽然此代码有效,但我将问题放在 * 评论中。
创建文件时:
String FILENAME = "myFile.txt";
String string = "just something";
// create a File object for the parent directory
File myDirectory = new File("/sdcard/myDir/"); // ******** <- what do I have to put HERE for standard data folder????
// have the object build the directory structure, if needed.
myDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(myDirectory, FILENAME);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = null;
//always have to put try/catch around the code - why? I don't know
try {
fos = new FileOutputStream(outputFile);
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//again have to put try/catch around it - otherwise compiler complains
try {
fos.write(string.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
发送文件时:
public void doSendFile() {
String fileName = "/sdcard/myDir/myFile.txt"; // ******** <- what do I have to put HERE for standard data folder????
Intent i = new Intent(Intent.ACTION_SEND);
try {
mainDataManager.logFileHandle.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "to@someone.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "subject");
i.putExtra(Intent.EXTRA_TEXT, "text");
Log.i(getClass().getSimpleName(),
"logFile=" + Uri.parse("file://" + fileName));
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + fileName));
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getBaseContext(),
"There are no email clients installed.", Toast.LENGTH_SHORT)
.show();
}
}
我发现该文件在创建时似乎存储在“data/data/com.xxx.xxxx/databases/myFile.txt”中。 但是当我在附件中使用它时,什么都没有发送。
所以基本上我只需要知道如何将文件存储在本地内存中,然后从那里发送。因为不是每个人都可能有一个外部存储 SD 卡 - 我想。
谢谢!
【问题讨论】:
标签: android email email-attachments