作成したテキストファイルを電子メールの添付ファイルとして送信しようとしています - デフォルトのフォルダーから
-
28-10-2019 - |
質問
テキストファイルを作成してから添付ファイルとして送信するなど、簡単なことを試みています。 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カードを持っているわけではないので、私は仮定します。
ありがとう!
解決
これは、SDカードが保護されていないことであるため、StandArt Javaの方法でそこに書くことができます。
Androidファイルシステムに書き込むためには、すべてのファイルがアプリキーを使用して保護され、他のアプリが使用されないように保護されるため、これを使用することはできません。
詳細については、このチュートリアルを参照してください。
所属していません StackOverflow