2011-04-20 41 views
19

我打算通過創建一個日誌來存儲應用程序的執行結果並使用一段python代碼解析它並自動繪製一個圖表。在Android設備上創建和存儲日誌文件

該應用程序是一個WiFi fingerprinter,即它收集信息,如mac id,rss(接收到的信號強度和關於周圍環境中的wifi設備的等級(標準化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不適合這種情況)。我嘗試了內部存儲,但是即使在將文件的模式設置爲MODE_WORLD_READABLE之後,也無法read the file using Device File Explorer in Eclipse

我仍然對使用外部存儲來存儲日誌保持警惕。有關如何寫入設備的USB文件的任何教程都一定會有所幫助。

我想到構建要存儲的數據以便使用SQLite進行存儲。但是這會在數據之間建立許多不必要的關係(國外和國內)並使其變得複雜。如果沒有辦法,那麼here be dragons

基本上我想寫入一個文件(更容易我想)在設備和後者通過連接到它通過USB讀取它在我的系統。任何幫助如何做到這一點將不勝感激。

+1

請訪問http://code.google.com/p/microlog4android。 – 100rabh 2011-04-20 12:43:24

+0

@ 100rabh關於如何使用它的任何指針? – primpap 2011-04-20 19:10:16

+0

相關的問題在這裏,用簡單的答案顯示如何寫入SD卡:http://stackoverflow.com/questions/1756296/android-writing-logs-to-text-file – RenniePet 2015-11-02 15:47:38

回答

11

警惕與否,外部存儲仍然是唯一的出路。如果沒有設備上的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(適配器)和場valuse轉換爲字符串和使用的getBytes()傳遞給FileOutputStream中寫入功能。我已經做了一些研究,並且在Java/Android中有很多其他選項用於文件寫入...例如FileWriter Class,因此需要進一步研究。

+0

該文件io寫入外部存儲代碼的示例有助於。 :) – primpap 2011-04-20 07:28:18

+0

我會看看我能做什麼... Java是誠實不是我的第一語言,所以在C中的這樣的例子會更多我的速度。我會嘗試在Java中發佈一些東西,但它可能會被燒燬......我現在在Java中所做的所有io最多都是非常黑客。如果其他人想在此期間加入,它肯定會受到歡迎。具體來說......不幸的是,我還沒有做過任何文件追加。 – Maximus 2011-04-20 14:35:07

+0

感謝您的關注。我讀到外部存儲的文件io與java中的文件沒有區別。所以我可以管理它。 – primpap 2011-04-20 14:55:37

11

我使用了一種非常簡單的方法,通過創建FileWriter對象將字符串消息寫入日誌文件。

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(); 
      } 
+0

更好的例子,可能會自己借用這個方法。 – Maximus 2012-01-13 16:05:31

+1

'out.write(...)'語句後需要'out.close()'。否則它不會保存寫入。 (我認爲這是困難的方法。) – Randy 2014-04-05 15:39:57

+0

一旦你out.close()緩衝區,你就不能重寫文件了。也就是說,如果你在'createFileOnDevice'中的'out.close()',你需要在下一個'out.write()'之前在'writeToFile'中創建一個具有相同路徑的'FileWriter'。 – lanyusea 2015-08-03 08:28:17