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

4

2 回答 2

1

通过使用解决file.getAbsolutePath()

于 2012-10-16T12:20:24.817 回答
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

根据亚历克斯法伯..

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

他在现场...

希望这会帮助你。

谢谢..

于 2012-10-16T12:15:15.820 回答