Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

android - How to write entire Logcat in to sdcard?

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.

Not able to write logcat in to sdcard.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...