将Logcat从Android应用程序保存到txt文件

时间:2016-01-13 23:17:49

标签: android logging save logcat android-logcat

我正在尝试获取我在Android Studio中看到的所有logcat(例如此处http://i.imgur.com/IJkGcL4.png),以便直接在我的设备上保存为txt file

我到目前为止尝试的是onCreate中的代码:

File logFile = new File(LogcatDir.getAbsolutePath() + "/log.txt" );
try
{
    Process process = Runtime.getRuntime().exec( "logcat -c");
    process = Runtime.getRuntime().exec( "logcat -f " + logFile + " *:S MyActivity_BUT_WHAT_TO_PUT_HERE");
}
catch ( IOException e ) {e.printStackTrace();}

manifest file

<uses-permission android:name="android.permission.READ_LOGS" />

我故意做了解析错误,如

long test = Long.parseLong("z");

但实际上只有log.txt

--------- beginning of /dev/log/main
--------- beginning of /dev/log/system

有没有人知道,我有什么代码,我得到整个logcat?

1 个答案:

答案 0 :(得分:5)

这对我有用,它使用以下代码将其保存在主存储器中:

        Log.w("before","Logcat save");
      // File logFile = new File( + "/log.txt" );
        try {
            Process process = Runtime.getRuntime().exec("logcat -d");
            process = Runtime.getRuntime().exec( "logcat -f " + "/storage/emulated/0/"+"Logging.txt");
        }catch(Exception e)
        {
            e.printStackTrace();
        }

enter image description here

enter image description here