将 Logcat 保存到 Android 设备中的文本文件

新手上路,请多包涵

我在 android 设备中运行应用程序时发现了一些崩溃,这在模拟器中没有显示。所以我需要将 Logcat 保存在设备内存或 SD 卡中的文本文件中。你能建议我这样做的好方法吗?

原文由 Nithin Michael 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 841
2 个回答

在应用程序的开头使用 Application 类。这允许正确的文件和日志处理。

下面的代码在以下位置创建一个日志文件:

 /ExternalStorage/MyPersonalAppFolder/logs/logcat_XXX.txt

XXX 是以毫秒为单位的当前时间。每次运行应用程序时,都会创建一个新的 logcat_XXX.txt 文件。

 public class MyPersonalApp extends Application {

    /**
     * Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
     */
    public void onCreate() {
        super.onCreate();

        if ( isExternalStorageWritable() ) {

            File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyPersonalAppFolder" );
            File logDirectory = new File( appDirectory + "/logs" );
            File logFile = new File( logDirectory, "logcat_" + System.currentTimeMillis() + ".txt" );

            // create app folder
            if ( !appDirectory.exists() ) {
                appDirectory.mkdir();
            }

            // create log folder
            if ( !logDirectory.exists() ) {
                logDirectory.mkdir();
            }

            // clear the previous logcat and then write the new one to the file
            try {
                Process process = Runtime.getRuntime().exec("logcat -c");
                process = Runtime.getRuntime().exec("logcat -f " + logFile);
            } catch ( IOException e ) {
                e.printStackTrace();
            }

        } else if ( isExternalStorageReadable() ) {
            // only readable
        } else {
            // not accessible
        }
    }

    /* Checks if external storage is available for read and write */
    public boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
            return true;
        }
        return false;
    }

    /* Checks if external storage is available to at least read */
    public boolean isExternalStorageReadable() {
        String state = Environment.getExternalStorageState();
        if ( Environment.MEDIA_MOUNTED.equals( state ) ||
                Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) ) {
            return true;
        }
        return false;
    }
}

您需要 .manifest 文件中的应用程序类的正确权限和名称:

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

<application
    android:name=".MyPersonalApp"
    ... >

编辑:

如果你只想保存一些特定活动的日志..

代替:

 process = Runtime.getRuntime().exec("logcat -f " + logFile);

和:

 process = Runtime.getRuntime().exec( "logcat -f " + logFile + " *:S MyActivity:D MyActivity2:D");

原文由 Drunken Daddy 发布,翻译遵循 CC BY-SA 4.0 许可协议

adb shell logcat -t 500 > D:\logcat_output.txt

转到您的终端/命令提示符并导航到其中包含 adb 的文件夹(如果它尚未添加到您的环境变量中)并粘贴此命令。

t 是您需要查看的行数

D:\logcat_output.txt 是你的 logcat 将被存储的地方。

原文由 smophos 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题