これには簡単な答えがあると確信していますが、私は初めてで、これを理解できないようです。
データをテキスト ファイルに保存する必要があります。それを行うためのすべてのコードがありますが、パスとファイル名は今のところハードコードされています。ユーザーがファイル名を入力してからボタンを押す EditText フィールドがあります。ユーザーが入力した内容に基づいてパスとファイル名を作成したい。
基本的に、「/sdcard/」+Whateveruserentered.txt の事前に決定されたパス
わかりました、ここに簡単な答えがあります、
EditText に「myPath/myfile.txt」と入力したとします。
まず、「myPath」フォルダーを作成する必要があります ( path にもフォルダー名を指定していると想定しています)。
String fullPath = myEditText.getText().toString().trim();
String folderPath = fullPath.substring ( 0, fullPath.indexOf ( "/" ) );
String fileName = fullPath.substring ( fullPath.indexOf ( "/" ) + 1 );
// First Create folder by coding,
File folder = new File(Environment.getExternalStorageDirectory().toString() + folderPath );
if (!folder.exists())
{
folder.mkdirs();
}
// Note: your path must not have recursive folders like myPath1/myPath2/myFile.txt, otherwise you need to create folder in 2 steps.
// Now creating file
File file = new File(Environment.getExternalStorageDirectory().toString() + folderPath + fileName );
if ( !file.exists() )
{
success = file.createFile();
}
// Now your file is created, you can do writing code now onwards.