【问题标题】:where to save a custom log file in android?在哪里保存自定义日志文件在android中?
【发布时间】:2013-07-18 04:23:39
【问题描述】:

我创建了一个用于写入日志文件的类并且效果很好:

public class LogFile {

String route;
Context context;

public LogFile(Context context, String date) {
    this.context = context;
    this.route= context.getFilesDir() + "/log_"+date+".txt";
}

public void appendLog(String text){       

    File logFile = new File(ruta);

    if (!logFile.exists()){
        try{
            logFile.createNewFile();
        } 
        catch (IOException e){
            e.printStackTrace();
        }
    }
    try{
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
        buf.append(text);
        buf.newLine();
        buf.close();
    }
    catch (IOException e){
        e.printStackTrace();
    }
}

}

但这生成的路由是 /data/data/com.myAplication.myAplication/files/log_20130717.txt 而且我无法从我的设备访问此目录。如果设备出现问题,我需要访问此文件。通过平板电脑的文件资源管理器,我可以看到以下目录:/root/data 和 /root/Android/data。 android 是否有助于在这些目录中创建我的应用程序目录?否则,我试图将文件放在“sdcard/log_20130717.txt”中,但我的权限被拒绝。

你建议我做什么?

【问题讨论】:

  • 如果隐私不是问题,为什么不把它保存在 sdcard 上呢?而且,究竟什么是“sdcard/log.file”。是路径吗?您是否尝试过获取外部存储的路径(通过String exPath = Environment.getExternalStorageDirectory().getAbsolutepath()),然后将文件保存到 exPath + "/" + log.file?
  • 感谢这项工作。但是如果我的设备没有 SD 卡怎么办?
  • 对于我正在处理的应用程序,我将许多文件保存到内部存储器中。当这些文件需要更新时,我会从内部存储器中检索它们,更新它们,然后将它们写回。如果这是您正在寻找的,我可以发布带有代码示例的答案。
  • 是的,请。如果没有 SD 卡,这可以作为替代方案。谢谢你:)
  • 我忽略了您需要从应用程序外部访问 logFile 的事实。就我而言,我只需要从我的应用程序中读取/更新文件。所以,我的代码对你没有多大帮助。如果您需要查看logFile(s),您可以在Eclipse 中打开DDMS 透视图并在“文件资源管理器”下查看。

标签: android


【解决方案1】:

把它放在externalFilesDir(),别忘了得到android.permission.WRITE_EXTERNAL_STORAGE

【讨论】:

    【解决方案2】:

    您可以使用droidQuery API 轻松写入文件。例如,这会做你想做的事:

    $.write(text, FileLocation.EXTERNAL, "log_"+date+".txt", true, true);
    

    【讨论】:

      【解决方案3】:

      到目前为止,这对我有用。经过一番研究,我发现了如何将我的数据存储在路径 /root/Android/data/ 中:

      public class LogFile {
      
      String name;
      Context context;
      
      
      public LogFile(Context context, String name) {
      
          this.context = context;
          this.name="log_"+name+".txt";
      
      }
      
      public void appendLog(String text){       
      
          File logFile = new File(context.getExternalFilesDir(null), name);
      
      
          if (!logFile.exists()){
              try{
                  logFile.createNewFile();
              } 
              catch (IOException e){
                  e.printStackTrace();
              }
          }
          try{
              BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
              buf.append(text);
              buf.newLine();
              buf.close();
          }
          catch (IOException e){
              e.printStackTrace();
          }
      }
      

      }

      使用

      File logFile = new File(context.getExternalFilesDir(null), name);
      

      Android 会自动使用我的项目名称创建一个目录,获取: /root/Android/data/com.myAPP.myApp/files/ 。在文件中,我创建了我的自定义日志,甚至很重要,当我的应用程序被卸载时,这个目录被删除。此外,用户也可以访问此文件。

      【讨论】:

        猜你喜欢
        • 2014-03-19
        • 1970-01-01
        • 2013-04-14
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        相关资源
        最近更新 更多