次のコードを使用して、.txt ファイルを作成して共有できます。
File path = new File(Environment.getExternalStorageDirectory(), "Folder");
if (!path.exists()) {
path.mkdirs();
}
File exportFile = new File(path, fileName);
FileWriter writer = new FileWriter(exportFile);
writer.append(body);
writer.flush();
writer.close();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportFile));
startActivity(intent);
ただし、このファイルをSDカードに直接保存するのではなく、アプリのフォルダーに保存したいので、以下のコードに変更しました。ファイルが作成されず、エクスポートしようとしているアプリがファイルを表示できないため、機能していないようです。
File path = new File(this.getFilesDir(), "exports");
if(!path.exists()){
path.mkdir();
}
何か案は?私が試したことはすべてうまくいきません。ファイルは Android/data/[パッケージ名] フォルダーに保存されるはずです はい?
編集:わかりました、これを機能させることができませんでしたが、実際にはこれが私が使用したい方法ではないことがわかりました.Android/dataフォルダーにファイルを保存したかったので、使用する必要がありました:
getExternalFilesDir(null)