【问题标题】:Android App: Save a txt file that can be accessed through ExplorerAndroid App:保存一个txt文件,可以通过资源管理器访问
【发布时间】:2015-07-08 13:21:00
【问题描述】:

我正在尝试在我的应用程序运行期间创建一个 txt 文件,然后我想将其下载到我的计算机上并评估其内容。

我在内部和外部存储上都试过了,但之后(当我的平板电脑插入计算机时)我仍然无法找到文本文件。

private void writeToFile(String data) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("orp.txt",Context.MODE_WORLD_WRITEABLE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();

        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"orp2.txt");
        String tempS = file.getAbsolutePath();
        file.setReadable( true, false ); 

        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(data);
        bw.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

我发现能够找到(“orp.txt”或“orp2.txt”),而且在应用程序运行期间我没有收到任何错误,并且应用程序能够打开“orp.txt”。 txt”在稍后的时间,所以我知道它已经被创建了。

【问题讨论】:

  • 我确定有一个媒体可以在设备上创建文件列表,除非调用某个命令,否则它不会更新。将尝试为您恢复文章。它过去有时可以通过拔下设备并将其重新插入电脑或重新启动设备来工作
  • 谢谢,您的评论为我指明了正确的方向

标签: android saving-data


【解决方案1】:

除非使用 Eclipse 文件资源管理器,否则我无法找到创建的文件,或者可能是必须触发的媒体扫描,可以通过重新启动设备来完成。扫描设备上可用的文件。

可能是目录,这是我在代码中所做的。

            File f = new File(cxt.getExternalFilesDir(filepath), fileName);
            f.createNewFile();
            FileWriter writer = new FileWriter(f); 
            writer.append(manNo+System.getProperty("line.separator"));
            writer.append(dateTime);
            writer.flush();
            writer.close();

【讨论】:

    【解决方案2】:

    感谢您的输入,但我发现答案与媒体扫描仪有关,重新启动设备后,“orp2.txt”出现在我的下载文件夹中。

    这个答案和博客文章有帮助

    Android: save file to downloads that can be viewed later

    【讨论】:

    • 你能接受我的回答吗,正如我所说的重启会起作用。谢谢
    猜你喜欢
    • 2022-09-23
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多