如何将整个 Logcat 写入SD卡?

4
我需要将整个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写入sd卡。


请查找类似的问题:如何将LogCat日志输出到SD卡 - NovusMobile
你遇到了什么问题? - Lalit Poptani
2
我已经通过执行“/system/bin/logcat -b main -f /sdcard/logcat.txt”解决了这个问题。这里的“-b main”允许您选择备用的logcat缓冲区,即(main、radio、events)。而“-f”则将logcat缓冲区传输到sdcard。 - RanjitRock
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接