将 Logcat 保存到 Android 设备中的文本文件 [英] Saving Logcat to a text file in Android Device

查看:47
本文介绍了将 Logcat 保存到 Android 设备中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had found some crashes while running the application in android device, which is not showing in emulator. So i need to save the Logcat in a text file in my device's memory or SD card. Could you please suggest me good method to do this?

推荐答案

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

Use an Application class at the beginning of your app. That allows a proper file and log handling.

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

Code below creates a log file at the following location:

/ExternalStorage/MyPersonalAppFolder/logs/logcat_XXX.txt

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

XXX is the current time in milliseconds. Every time you run your app, a new logcat_XXX.txt file will be created.

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 文件中需要正确的权限和应用程序类的名称:

you need the correct permissions and name of your application class in your .manifest file:

<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"
    ... >

如果您只想保存某些特定活动的日志..

if you want to save log of only some particular activities..

替换:

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

与:

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

这篇关于将 Logcat 保存到 Android 设备中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆