如何写全的logcat到SD卡?如何写、logcat、SD

2023-09-04 09:43:06 作者:入眼皆是你

我需要在写入整个logcat中到SD卡未过滤的LogCat中。所以,我试图做这样

I need to write entire Logcat in to sdcard not the filtered Logcat. So I tried doing this way

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

我所给出的清单文件中的所有权限了。

I have given all permissions in manifest file too.

不能够到SD卡写入logcat中。

Not able to write logcat in to sdcard.

推荐答案

您可以 教程读取日志。而对于写入日志文件中,你可以简单地使用的FileWriter 放弃与文件名的文件的路径。

You can have this Tutorial for Reading the Logs. And for writing the logs in the file you can simply use FileWriter give path of the file with file name.

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