Reputation: 43331
static final String FILE_LOG = "log.txt";
private void SaveLogToExternalStorage()
{
String s = tv_log.getText().toString();
File file;
FileOutputStream fos = null;
try
{
file = new File(getExternalFilesDir(null), FILE_LOG);
fos = new FileOutputStream(file);
}
catch(FileNotFoundException e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
try
{
fos.write(s.getBytes());
fos.close();
}
catch(IOException e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
String savedFile = Environment.getExternalStorageDirectory() + "/" + FILE_LOG;
Toast.makeText(this, "Log is saved to " + savedFile, Toast.LENGTH_SHORT).show();
}
This function prints Log is saved to mnt/sdcard/log.txt
Actually the file is saved to mnt/sdcard/Android/data/package.name/files/log.txt
How can I find this directory programmatically to show correct message?
Upvotes: 2
Views: 5992
Reputation: 2082
Just change following line.
String savedFile1 = Environment.getExternalStorageDirectory() + getFileStreamPath(FILE_LOG).toString();
Toast.makeText(this, "Log is saved to " + savedFile1, Toast.LENGTH_LONG).show();
Log.d("File", savedFile1);
This will gives output like: /mnt/sdcard/data/data/com.android.experimentalproject/files/log.txt
And as per Alex Farber..
String savedFile = file.getAbsolutePath().toString();
Toast.makeText(this, "Log is saved to " + savedFile, Toast.LENGTH_SHORT).show();
Log.e("PATH", savedFile1);
Output: /mnt/sdcard/Android/data/com.android.experimentalproject/files/log.txt
He is spot on...
Hope this will help you.
Thanks..
Upvotes: 0