【问题标题】:file disappearing from SD card文件从 SD 卡中消失
【发布时间】:2012-06-10 00:36:54
【问题描述】:

我在 SD 卡上创建文本文件以附加到要通过 gmail 应用程序发送的电子邮件时遇到问题。 当附加到 gmail 应用程序中的电子邮件时,该电子邮件将永远处于红色的“正在发送...”状态。该文件是使用下面的 createCSVfile() 创建的。

调试我的代码,在不同时间启动我的应用程序,csv_file.exists() 总是返回 false,就好像该文件未找到并且每次运行应用程序时都会创建一样。 但是,使用文件管理器我可以看到文件在运行之间和运行期间存在。

有什么帮助吗? 谢谢

File csv_file = null;
String createCSVfile() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        csv_file = new File( getExternalFilesDir(null) + File.separator + "InOutStats.txt");
        if (csv_file != null ) {
            if( csv_file.exists() ){
                Log.v("CSV_FILE", "Stat file " + csv_file.toString() +" already there!");
            }else{
                csv_file.getParentFile().mkdirs();
                try {
                    boolean bool = csv_file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            FileWriter fWriter = null;
            try {
                fWriter = new FileWriter(csv_file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            BufferedWriter writer = new BufferedWriter(fWriter);
            try {
                writer.write("Some text here!!! " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
                writer.newLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }else{
        Log.v("CSV_FILE", "NO SD CARD HERE???");
    }
    return csv_file.toString();
}

【问题讨论】:

    标签: android android-sdcard fileinfo


    【解决方案1】:

    错误是:

    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis())
    

    应该是

    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
    

    我只看到了两个非常小的“错误”:

    [风格问题]

    csv_file = new File( getExternalFilesDir(null) + File.separator + "InOutStats.txt");
    

    应该是

    csv_file = new File( getExternalFilesDir(null), "InOutStats.txt");
    

    因为否则您使用的是File.toString()

    [最小码]

    删除应该是:

    csv_file.createNewFile();
    

    第二次尝试

    尝试替换

        if (csv_file != null ) {
            if( csv_file.exists() ){
                Log.v("CSV_FILE", "Stat file " + csv_file.toString() +" already there!");
            }else{
                csv_file.getParentFile().mkdirs();
                try {
                    boolean bool = csv_file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    

         {
    

    这将删除存在测试、mkdirs 和不需要的单独文件创建。 已完成尝试限制错误区域。

    此外,您正在使用文本的默认平台编码;你可以明确表示:

    new FileWriter(csv_file, "UTF-8")
    

    【讨论】:

    • 嗨。我按照您的建议应用了这两个更改,但一切都是一样的。事实上,当我只在文件中打印一个静态字符串并且可以给 new File() 提供整个路径时,也会发生同样的问题。
    • 你好。很奇怪。我在另外两部手机上测试了相同的代码,邮件发送正确。不知道发生了什么。谢谢
    • 可能是物理文件系统被缓冲了,没有直接写入磁盘/SD卡。在其他手机上测试的好主意。
    • 嗨乔普。文件在磁盘上,在正确的位置。即使应用程序未运行,我也可以使用文件管理器读取它。手机是中兴的刀片(我知道,硬件有点便宜)。
    猜你喜欢
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多