试图发送一个创建的文本文件作为电子邮件附件 - 从默认文件夹 [英] Trying to send a created text file as an email attachment - from a default folder
问题描述
我想一些简单的创建一个文本文件,然后把它作为附件。虽然如果我使用的SD卡能正常工作,我不知道在哪里把它的标准数据文件夹,让我的应用程序的实际工作为大家没有SD卡(文件是有点不可见)
I am trying something simple as creating a text file and then send it as attachment. While it works fine if I use the sdcard I don't know where to put it in the "standard data folder" so my app works actually for everyone without sdcard (and the file is somewhat invisible)
虽然这code的作品,我把这些问题在< - * 注释
While this code works, I put the questions in the <- * comment.
在创建文件:
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();
}
}
我发现该文件似乎是存放在数据/数据/ com.xxx.xxxx /数据库/在创建MYFILE.TXT。
但是,当我用这个在附件中,什么也没有送。
I found that the file seems to be stored at "data/data/com.xxx.xxxx/databases/myFile.txt on creation. But when I used this in the attachment, nothing was sent.
因此,基本上所有我需要的是知道如何存储在本地内存中的文件,然后从那里发出。因为不是每个人都可能有外部存储SD卡 - 我假设
So basically all I need is to know how to store a file in local memory and then send it from there. Since not everyone might have an external memory sd card - I assume.
谢谢!
推荐答案
这是监守SD卡不被保护,所以你可以像你一样的非标准Java的方式写入到那里。
This is becuase the SD card is not protected, so you can write to there in the standart java way as you did.
为了给你写信不能使用这个Android的文件系统,因为每个文件使用一个应用程序键,以避免其他应用程序使用它的保护。
In order to write to the Android file system you cannot use this, since every file is protected using an App key to avoid other apps to use it.
您使用768,16 openFileOutput()请参见本教程的详细信息:
You shoud use openFileOutput() see this tutorial for more info:
http://www.anddev.org/working_with_files-t115.html
这篇关于试图发送一个创建的文本文件作为电子邮件附件 - 从默认文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!