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();
}
此函數打印Log is saved to mnt/sdcard/log.txt
其實我如何才能找到這個目錄編程,以顯示正確的消息的文件保存到mnt/sdcard/Android/data/package.name/files/log.txt
?如何找到外部存儲目錄
解決getExternalFilesDir和getExternalStorageDirectory是明顯不一樣的功能。你也可以從'file'獲得路徑。 – Zharf