如何找到外部存储目录
问题描述:
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();
}
这个函数打印日志将被保存到MNT / SD卡/ log.txt的
其实,文件保存到 MNT / SD卡/ Android的/数据/ package.name/files/log.txt
如何编程找到这个目录显示正确的信息?
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?
答
通过解决 file.getAbsolutePath()