在Android设备上创建和存储日志文件

| 我打算通过创建一个日志来存储应用程序执行的一些结果来自动化应用程序的测试,然后使用一段python代码对其进行解析并绘制图形。 该应用程序是一个WiFi指纹识别器,即它收集有关周围环境中wifi设备的信息,例如mac id,rss(收到的信号强度和排名(标准化的rss))。因此,要测试此应用程序,我必须将其带到该位置并记录结果(截至目前为手动),因此logcat不能达到目的。 自动化要求 1.将日志存储在设备中 2.通过USB访问系统中的日志文件 日志文件格式:
Snapshot: 1
Fingerprint: 1, Rank: 0.23424, Boolean: true
Fingerprint: 2, Rank: 0.42344, Boolean: false
Fingerprint: 3, Rank: 0.23425, Boolean: true

Snapshot: 2
Fingerprint: 1, Rank: 0.75654, Boolean: false
Fingerprint: 2, Rank: 0.23456, Boolean: true
Fingerprint: 3, Rank: 0.89423, Boolean: true 

................
现在我知道持久性存储基本上有3种方法(无论如何,SharedPrefs都不适合这种情况)。我尝试了内部存储,但是即使将文件的模式设置为“ 1”,也无法使用Eclipse中的设备文件资源管理器读取文件。 我仍然对使用外部存储来存储日志保持警惕。任何有关如何在设备的USB中写入文件的教程都绝对会有所帮助。 我想到了结构化要存储的数据,以便使用SQLite进行存储。但这在数据之间建立了许多不必要的关系(外国和国内)并使之变得复杂。如果没有办法,那就是龙。 基本上,我想写入设备中的文件(我想更容易),而后者则通过USB连接到我的系统中以读取它。对此方法的任何帮助将不胜感激。
已邀请:
不管是否担心,外部存储仍然可能是唯一的方法。没有设备上的root访问权限,您将无法真正获得“内部”任何东西,除非您可以在设备上的应用程序中进行读取。该文档为创建外部文件的位置提供了相当扎实的指导,如果您使用的是API Level 8或更高版本,则可以使用几个额外的功能。我确定您知道此页面,但是无论如何这里是:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal 如果您需要任何文件io示例代码...我想我可以挖掘一些... 编辑-我将从遵循上述文档中的指南开始,首先确认存储状态。不幸的是,我没有用Java附加文件的经验,因此肯定会有其他人回答。这不包括追加,但是我的一个个人应用程序中有一个备份例程,看起来像这样。
    File backupPath = Environment.getExternalStorageDirectory();

    backupPath = new File(backupPath.getPath() + \"/Android/data/com.maximusdev.bankrecord/files\");

    if(!backupPath.exists()){
        backupPath.mkdirs();
    }

    FileOutputStream fos;
    try {
        fos = new FileOutputStream(backupPath.getPath() + \"/recordsbackup.txt\");

        if(okaytowrite){
            for(int i = 0; i < count; ++i){
                entry = adapter.getItem(i);
                fos.write(entry.toString().getBytes());
                fos.write(\"\\n\".getBytes());
                fos.write(String.valueOf(entry.dateTime).getBytes());
                fos.write(\"\\n\".getBytes());
                fos.write(String.valueOf(entry.sign).getBytes());
                fos.write(\"\\n\".getBytes());
                fos.write(String.valueOf(entry.cleared).getBytes());
                fos.write(\"\\n\".getBytes());
                fos.write(String.valueOf(entry.transDate).getBytes());
                fos.write(\"\\n\".getBytes());
                fos.write(entry.category.getBytes());
                fos.write(\"\\n\".getBytes());
            }
        }
        fos.close();

        Toast.makeText(this, \"Backup Complete\", Toast.LENGTH_SHORT).show();

    } catch (FileNotFoundException e) {

        e.printStackTrace();

        AlertDialog.Builder delmessagebuilder = new AlertDialog.Builder(this);

        delmessagebuilder.setCancelable(false);

        delmessagebuilder.setMessage(\"File Access Error\");

        delmessagebuilder.setNeutralButton(\"Okay\", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });

        delmessagebuilder.create().show();

    } catch (IOException e) {

        e.printStackTrace();

        AlertDialog.Builder delmessagebuilder = new AlertDialog.Builder(this);

        delmessagebuilder.setCancelable(false);

        delmessagebuilder.setMessage(\"File Access Error\");

        delmessagebuilder.setNeutralButton(\"Okay\", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });

        delmessagebuilder.create().show();
    }
一旦准备好编写,就将自定义对象(条目)从ArrayAdapter(适配器)中拉出并将字段值转换为字符串,并使用getBytes()传递给FileOutputStream写入函数。我已经进行了一些研究,并且在Java / Android中还有许多其他文件编写选项……例如FileWriter类,因此它有待进一步研究。
我使用一种非常简单的方法,通过创建FileWriter对象,将String消息写入日志文件。
    public static BufferedWriter out;
        private void createFileOnDevice(Boolean append) throws IOException {
                /*
                 * Function to initially create the log file and it also writes the time of creation to file.
                 */
                File Root = Environment.getExternalStorageDirectory();
                if(Root.canWrite()){
                     File  LogFile = new File(Root, \"Log.txt\");
                     FileWriter LogWriter = new FileWriter(LogFile, append);
                     out = new BufferedWriter(LogWriter);
                     Date date = new Date();
                     out.write(\"Logged at\" + String.valueOf(date.getHours() + \":\" + date.getMinutes() + \":\" + date.getSeconds() + \"\\n\")); 
                     out.close();

            }
        }
现在,该功能可以将新消息写入日志文件。
    public void writeToFile(String message){
            try {
                out.write(message+\"\\n\");
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

要回复问题请先登录注册