如何将整个Logcat写入sdcard?

时间:2012-01-24 05:08:22

标签: android logcat

我需要将整个Logcat写入sdcard而不是过滤后的Logcat。所以我试着这样做

String[] cmd  ={"/system/bin/logcat " +"-f"+" /sdcard/d.txt"};
    Log.d("NTK","cmd string"+cmd);
    Runtime run = Runtime.getRuntime();

    Process pr;
    try {
        pr = run.exec(cmd);

    pr.waitFor();
    BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line = "";
    while ((line=buf.readLine())!=null) {
        System.out.println(line);
        Log.i(NTL, "lines"+line);
    }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我也在清单文件中提供了所有权限。

无法将logcat写入sdcard。

1 个答案:

答案 0 :(得分:2)

您可以拥有this阅读日志的教程。要在日志中写入日志,您只需使用FileWriter给文件路径指定文件名。

try {
            File root = Environment.getExternalStorageDirectory();
            if (root.canWrite()) {
                File gpslogfile = new File(root, "log.txt");
                FileWriter gpswriter = new FileWriter(gpslogfile, true);
                BufferedWriter out = new BufferedWriter(gpswriter);
                out.append(DataToWrite);
                out.close();
            }
        } catch (IOException e) {
            Log.d("Log: ", "Could not write file " + e.getMessage());
        }