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

Save Logcat from Android app to txt file

我正在尝试获取我在 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?

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

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