在android中在何处保存自定义日志文件?

在android中在何处保存自定义日志文件?,android,Android,我创建了一个用于编写日志文件的类,效果非常好: 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 te

我创建了一个用于编写日志文件的类,效果非常好:

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”中,但我的权限被拒绝

你建议我怎么做?

把它放进去,别忘了获得
android.permission。写外部存储
把它放进去,别忘了获得
android.permission。写外部存储
你可以使用API轻松地写文件。例如,这将执行您想要的操作:

$.write(text, FileLocation.EXTERNAL, "log_"+date+".txt", true, true);
您可以使用API轻松地写入文件。例如,这将执行您想要的操作:

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

到目前为止,这对我是有效的。经过进一步研究,我发现如何将数据存储在路径/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/。在文件中,我创建了自定义日志,更重要的是,当卸载我的应用程序时,该目录将被删除。此外,用户还可以访问此文件。

到目前为止,这对我来说是有效的。经过进一步研究,我发现如何将数据存储在路径/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/。在文件中,我创建了自定义日志,更重要的是,当卸载我的应用程序时,该目录将被删除。此外,用户还可以访问此文件。

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