我正在尝试一些简单的事情,例如创建一个文本文件,然后将其作为附件发送。虽然如果我使用 SD 卡它工作正常,但我不知道将它放在“标准数据文件夹”中的哪里,所以我的应用程序实际上适用于没有 SD 卡的每个人(并且该文件有点不可见)
当这段代码有效时,我将问题放在 *评论。
创建文件时:
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[] { "[email protected] /cdn-cgi/l/email-protection" });
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 卡。
Thanks!