如何查找外部存储目录

时间:2012-10-16 12:00:10

标签: android file-io

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如何以编程方式找到此目录以显示正确的消息?

2 个答案:

答案 0 :(得分:1)

使用file.getAbsolutePath()

解决

答案 1 :(得分:0)

只需改变以下行。

String savedFile1 = Environment.getExternalStorageDirectory() + getFileStreamPath(FILE_LOG).toString();
Toast.makeText(this, "Log is saved to " + savedFile1, Toast.LENGTH_LONG).show();
Log.d("File", savedFile1);

这将提供如下输出:/mnt/sdcard/data/data/com.android.experimentalproject/files/log.txt

根据Alex Farber的说法..

String savedFile = file.getAbsolutePath().toString();
Toast.makeText(this, "Log is saved to " + savedFile, Toast.LENGTH_SHORT).show();
Log.e("PATH", savedFile1);

输出:/mnt/sdcard/Android/data/com.android.experimentalproject/files/log.txt

他现场......

希望这会对你有所帮助。

谢谢..